VBA在单元格更改时发送电子邮件

时间:2018-08-29 10:23:23

标签: excel vba excel-vba

这很好用,但仅当直接更改单元格以擦除公式时才有效。我范围内的每个单元格都包含一个VLOOKUP公式,该公式可从单独工作表中的一长串数据中获取值。这些值是从数据库导入的,并且每隔一段时间刷新一次。基本上,我需要一种当单元格通过公式更改时触发电子邮件的替代方法。我希望这是有道理的。

Dim xRg As Range
'Update by Extendoffice 2018/3/7
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
    Set xRg = Intersect(Target, Range("N1:N999"))
    If xRg Is Nothing Then Exit Sub
    If (Range("N45") = Range("F45")) Then
        Call Mail_small_Text_Outlook
    End If
End Sub
Sub Mail_small_Text_Outlook()
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Hi there" & vbNewLine & vbNewLine & _
          xRg.Offset(0, -12) & " has reached its target"

    On Error Resume Next
    With xOutMail
        .To = "***@****.com"
        .CC = ""
        .BCC = ""
        .Subject = "Target Reached"
        .Body = xMailBody
        .Send   'or use .Display
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

3 个答案:

答案 0 :(得分:1)

我希望它会有所帮助。我尽我所能理解问题的程度尝试了代码,但是如果出现错误,您可以对其进行修改

axios.defaults.headers.common['Authorization'] = 'Bearer ' + value;

答案 1 :(得分:0)

您可以使用Worksheet_Calculate()事件。每次计算公式时,都会触发该事件

答案 2 :(得分:0)

rank()