[英]VBA email code triggered by Cell Value based off of a Formula
我對 Excel 中的 VBA 非常陌生,只是復制了我在互聯網上找到的這段代碼。 問題是目前 email 僅在將 Q 列中的值手動更改為 30 時才發送,但是,我有一個公式用於此單元格編號自動更新(它是運行天數),所以我希望它根據公式觸發 VBA,無需手動將值輸入單元格。 任何幫助都會很棒!
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("Q2:Q6000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value = 30 Then
Call Mail_small_Text_Outlook(Range("A" & Target.Row).Value)
End If
End Sub
Sub Mail_small_Text_Outlook(mailSubject As String)
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "mail body"
On Error Resume Next
With xOutMail
.To = "email"
.CC = ""
.BCC = ""
.Subject = mailSubject
.Body = xMailBody
.Send 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Private Sub Worksheet_Calculate()
Dim xI As Integer
Dim xRg As Range
Set xRg = Range("Q2:Q6000")
On Error GoTo Err01
xI = Int(xRg.Value)
If xI = 30 Then
Call Mail_small_Text_Outlook(Range("A" & Target.Row).Value)
End If
Err01:
End Sub
嘗試這個:
Private Sub Worksheet_Calculate()
Const CHECK_RANGE As String = "Q2:Q6000"
Const INFO_COL As String = "A" 'info for the email
Const FLAG_COL As String = "R" 'column to store "sent mail" date
Dim c As Range, v, dt
For Each c In Me.Range(CHECK_RANGE).Cells
v = c.Value
If IsNumeric(v) Then
If v = 30 Then
'already sent a mail today?
If c.EntireRow.Columns(FLAG_COL).Value <> Date Then
c.EntireRow.Columns(FLAG_COL).Value = Date 'record send date
Debug.Print c.Address 'for testing
'Mail_small_Text_Outlook c.EntireRow.Columns(INFO_COL).Value
End If
End If
End If
Next c
End Sub
如果應該每天每個單元格只發送一封郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.