Skip to main content

如何在Excel中當截止日期到達時發送電子郵件?

Author: Siluvia Last Modified: 2025-05-12

如下圖截屏所示,如果C列中的截止日期小於或等於7天(例如,當前日期為2017/9/13),則會向A列中的指定收件人發送電子郵件,並且B列中的指定內容將顯示在郵件正文中。您該如何實現這一功能呢?本文提供了一段VBA代碼來幫助您完成此任務。

sample data

使用VBA代碼在截止日期到達時發送電子郵件


使用VBA代碼在截止日期到達時發送電子郵件

請按照以下步驟操作,以便在Excel中截止日期到達時發送電子郵件提醒。

1. 同時按下 Alt + F11 鍵以打開 Microsoft Visual Basic for Applications 窗口。

2. 在 Microsoft Visual Basic for Applications 窗口中,請點擊 插入 > 模組。然後將以下VBA代碼複製並粘貼到模組窗口中。

VBA代碼:在Excel中截止日期接近時發送電子郵件

Public Sub CheckAndSendMail()
'Updated by Extendoffice 2018/11/22
    Dim xRgDate As Range
    Dim xRgSend As Range
    Dim xRgText As Range
    Dim xRgDone As Range
    Dim xOutApp As Object
    Dim xMailItem As Object
    Dim xLastRow As Long
    Dim vbCrLf As String
    Dim xMailBody As String
    Dim xRgDateVal As String
    Dim xRgSendVal As String
    Dim xMailSubject As String
    Dim i As Long
    On Error Resume Next
    Set xRgDate = Application.InputBox("Please select the due date column:", "KuTools For Excel", , , , , , 8)
    If xRgDate Is Nothing Then Exit Sub
    Set xRgSend = Application.InputBox("Please select the recipients?email column:", "KuTools For Excel", , , , , , 8)
    If xRgSend Is Nothing Then Exit Sub
    Set xRgText = Application.InputBox("Select the column with reminded content in your email:", "KuTools For Excel", , , , , , 8)
    If xRgText Is Nothing Then Exit Sub
    xLastRow = xRgDate.Rows.count
    Set xRgDate = xRgDate(1)
    Set xRgSend = xRgSend(1)
    Set xRgText = xRgText(1)
    Set xOutApp = CreateObject("Outlook.Application")
    For i = 1 To xLastRow
        xRgDateVal = ""
        xRgDateVal = xRgDate.Offset(i - 1).Value
        If xRgDateVal <> "" Then
        If CDate(xRgDateVal) - Date <= 7 And CDate(xRgDateVal) - Date > 0 Then
            xRgSendVal = xRgSend.Offset(i - 1).Value
            xMailSubject = xRgText.Offset(i - 1).Value & " on " & xRgDateVal
            vbCrLf = "<br><br>"
            xMailBody = "<HTML><BODY>"
            xMailBody = xMailBody & "Dear " & xRgSendVal & vbCrLf
            xMailBody = xMailBody & "Text : " & xRgText.Offset(i - 1).Value & vbCrLf
            xMailBody = xMailBody & "</BODY></HTML>"
            Set xMailItem = xOutApp.CreateItem(0)
            With xMailItem
                .Subject = xMailSubject
                .To = xRgSendVal
                .HTMLBody = xMailBody
                .Display
                '.Send
            End With
            Set xMailItem = Nothing
        End If
    End If
    Next
    Set xOutApp = Nothing
End Sub

注意:VBA代碼中的這一行 If CDate(xRgDateVal) - Date <= 7 And CDate(xRgDateVal) - Date > 0 Then 表示截止日期必須大於1天且小於或等於7天。您可以根據需要進行更改。

3. 按下 F5 鍵運行代碼。在第一個彈出的 Kutools for Excel 對話框中,請選擇截止日期列範圍,然後點擊 確定 按鈕。參見截圖:

vba code to select the due date column

4. 接著第二個 Kutools for Excel 對話框彈出,請選擇包含收件人電子郵件地址的對應列範圍,並點擊 確定 按鈕。參見截圖:

 vba code to select the recipients’ email addresses

5. 在最後一個 Kutools for Excel 對話框中,選擇要在郵件正文中顯示的內容,然後點擊 確定 按鈕。

vba code to select the content you want to display in the email body

然後,如果C列中的截止日期小於或等於7天,將自動創建一封電子郵件,列出指定的收件人、主題和正文。請點擊 發送 按鈕發送電子郵件。

an email is created  with the specified recipient, subject and body listed out if the due date

注意

1. 每封創建的電子郵件都對應一個截止日期。例如,如果有三個截止日期符合條件,將自動創建三封電子郵件。

2. 如果沒有符合條件的日期,此代碼不會被觸發。

3. 該VBA代碼僅在您使用Outlook作為電子郵件程序時有效。

a screenshot of kutools for excel ai

使用 Kutools AI 解鎖 Excel 的魔法

  • 智能執行:執行單元格操作、分析數據並創建圖表——所有這些都由簡單的指令驅動。
  • 自訂公式:生成量身定制的公式,簡化您的工作流程。
  • VBA 編碼:輕鬆編寫和實現 VBA 代碼。
  • 公式解釋:輕鬆理解複雜的公式。
  • 文本翻譯:打破電子表格中的語言障礙。
通過人工智能工具增強您的 Excel 能力。立即下載,體驗前所未有的效率!

相關文章:

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!