【问题标题】:How can I send an automatic e-mail when any of certain range of cells' value has changed当任何特定范围的单元格的值发生更改时,如何发送自动电子邮件
【发布时间】:2017-03-25 22:01:47
【问题描述】:

如何编写一个 Excel 宏,当某个单元格的某个范围的值发生更改时,它会自动发送一封电子邮件?

问题是我选择的单元格范围具有直接链接到其他电子表格单元格的公式。这些单元格的数据已通过 Excel 的 Web 连接查询进行了更新。如下图所示,a1:b5 范围有公式链接到 d1:e5 范围。

这是我的语法

Private Sub Worksheet_Change(ByVal Target As Range)


    Dim rngChangeCells As Range
    Dim objOutlookApp As Outlook.Application
    Dim objMailItem As Outlook.MailItem
    Dim strMailBody As String

    On Error Resume Next
    Set rngChangeCells = Intersect(Target, Me.Range("a1:b5"))
    On Error GoTo 0

    If Not rngChangeCells Is Nothing Then

        Set objOutlookApp = New Outlook.Application
        Set objMailItem = objOutlookApp.CreateItem(olMailItem)

        strMailBody = "Cell(s) " & rngChangeCells.Address(False, False) & _
            " in the worksheet '" & Me.Name & "' were modified on " & _
            Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
            " by " & Environ$("username") & "."

        With objMailItem
            .To = "myagmarchuluun@gmail.com"
            .Subject = "It has changed"
            .Body = strMailBody
            .Display
        End With

        Set rngChangeCells = Nothing
        Set objOutlookApp = Nothing
        Set objMailItem = Nothing

    End If

End Sub

enter image description here

【问题讨论】:

  • 那么问题或错误是什么?
  • 是不是查询刷新没有触发change事件?查询表有一个刷新后事件Private Sub q_AfterRefresh(ByVal Success As Boolean)
  • a1:b5 上的数据具有链接到其他工作表的其他单元格的公式。
  • 类似于 a1=sheet2$b$1,如果我手动更改了 a1 单元格的值,则宏已成功运行并显示了我的电子邮件项目。但是 Sheet2$b$1 单元格(具有来自网站的数据连接)的值发生了变化,宏无法正常工作:(

标签: excel vba


【解决方案1】:

类似的东西。

注意:将 YourMacroName 更改为代码中宏的名称。 如果您希望代码适用于另一个或更多单元格,您可以更改事件的范围。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
        If IsNumeric(Target.Value) And Target.Value > 200 Then
            Call YourMacroName
        End If
    End If
End Sub

测试此示例宏以创建/显示带有小文本消息的 Outlook 邮件。 您必须在标准模块中复制此宏,而不是在工作表模块中,请参阅此页面。

注意:我在代码中使用 .Display 来显示邮件,您可以将其更改为 .Send

不要忘记在 Change 事件中将 Call YourMacroName 更改为 Call Mail_small_Text_Outlook。

Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Excel 2000-2016
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strbody = "Hi there" & vbNewLine & vbNewLine & _
              "Cell A1 is changed" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "This is line 4"

    On Error Resume Next
    With OutMail
        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = strbody
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

https://www.rondebruin.nl/win/s1/outlook/bmail9.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    相关资源
    最近更新 更多