当单元格更改时触发电子邮件

Trigger email when cell changes

提问人:shariffa 提问时间:7/25/2023 最后编辑:Communityshariffa 更新时间:8/3/2023 访问量:79

问:

我试图在更改单元格颜色时触发带有附件的电子邮件。

问题是我正在尝试从不同的单元格中检索信息。

如果我在C7中更改颜色,xMailBody将是来自A8,C5和B3的数据。
这将返回 Shariffa,2 Aug 23。

如果我将 H31 中的颜色更改为 K31,xMailBody 将是 A31、H29 到 K29 和 B27。
这将返回 Rae,23 年 11 月 7 日至 10 日。

Excel 的外观。
enter image description here

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim xDateSelected As Range
    Dim xOutApp As Object
    Dim xMailItem As Object
    Dim xMailBody As String
    
    On Error Resume Next
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Set xDateSelected = Range("date1").Value
    Set Mydate = Intersect(Target, xDateSelected)
    ActiveWorkbook.Save
    
    
    If Target.Interior.Color = RGB(255, 0, 0) Then

    ' Set this to the exact color or flip the statement so it's:
    ' If Target.Interior.Color <> RGB(255, 0, 0) Then
    
    Dim r As Integer
    Dim c As Integer
    Dim staff As String
    Dim date1 As String
    
    r = 0
    c = 0
    While Target.Offset(r, 0) <> ""
        r = r - 1
    Wend
    While Target.Offset(0, c) <> ""
        c = c - 1
    Wend
    
    'These move through the row (and then the column) until a non empty cell is found

    staff = Target.Offset(0, c).Value
    date1 = Target.Offset(r, 0).Value & Target.Offset(r - 2, 0).Value

    'Get the string values; need to append the two day then date values

    End If
    
    If Not Mydate Is Nothing Then
        Set xOutApp = CreateObject("Outlook.Application")
        Set xMailItem = xOutApp.CreateItem(0)

        'code to retrieve content from affected cells and putting into email content ie "staff name" is applying for leave on "date"
      
        xMailBody = "Hi there Priscilla" & vbNewLine & vbNewLine & _
        "Name: " & Range("A" & Target.Row).Value & " is applying for Ad-hoc leave on " & Range("date1" & Target.Row).Value & vbNewLine & vbNewLine & _
        "Reason: " & vbNewLine & vbNewLine & _
        "Thank you" & vbNewLine 'calling out and placing values of each col into email body

        With xMailItem
            .To = "[email protected]"
            .Subject = "Applying for Ad-hoc leave "
            .Body = xMailBody
            .Attachments.Add (ThisWorkbook.FullName)
            .Display
        End With
        Set xRgSel = Nothing
        Set xOutApp = Nothing
        Set xMailItem = Nothing
    End If
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub
Excel VBA 电子邮件

评论


答:

0赞 Alexander 7/25/2023 #1

首先,您尚未发布您正在使用的确切子,但假设它是Worksheet_Change,它不会因单元格格式更改而触发。相反,请使用Worksheet_SelectionChange。

如果您假设除了要提取的标题外,单元格将始终为空,则迭代直到到达非空单元格将起作用:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Interior.Color = RGB(255,255,0) Then 
' Set this to the exact color or flip the statement so it's:
' If Target.Interior.Color <> RGB(0,0,0) Then
    Dim r As Integer
    Dim c As Integer
    Dim staff As String
    Dim date As String
    r = 0
    c = 0
    While Target.Offset(r,0) <> ""
        r = r - 1
    Wend
    While Target.Offset(0,c) <> ""
        c = c - 1
    Wend
' These move through the row (and then the column) until a non empty cell is found
    staff = Target.Offset(0,c).Value
    date = Target.Offset(r,0).Value & Target.Offset(r-2,0).Value
' Get the string values; need to append the two day then date values
End If
End Sub
    

让我知道这是否有效。

编辑:在选择多个日期的情况下,每次更改颜色时都会触发 Change 事件。考虑一下,最好创建一个 FormControl 按钮并将宏链接到该按钮,这样您就不会在更改单元格颜色后立即发送电子邮件。

编辑2:这是修改后的问题中的更新代码。让我知道这是否有效。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

' Start with the conditional
If Target.Interior.Color = RGB(255,0,0) Then

Dim xDateSelected As Range
Dim xOutApp As Object
Dim xMailItem As Object
Dim xMailBody As String

On Error Resume Next

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim r As Integer
Dim c As Integer
Dim staff As String
Dim date1 As String

r = 0
c = 0
While Target.Offset(r, 0) = ""
    r = r - 1
Wend
While Target.Offset(0, c) = ""
    c = c - 1
Wend
'These move through the row (and then the column) until a non empty cell is found

staff = Target.Offset(0, c).Value
date1 = Target.Offset(r, 0).Value & Target.Offset(r - 2, c).Value
'I use r-2, c here to get the date, assuming it is a merged cell which have weird behaviour
'Get the string values; need to append the two day then date values

Set xDateSelected = Range(date1).Value ' Keep in mind you must either reference the cell itself 
' or a variable storing a string with the cell reference

Set Mydate = Intersect(Target, xDateSelected)
ActiveWorkbook.Save

If Not Mydate Is Nothing Then
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailItem = xOutApp.CreateItem(0)
'code to retrieve content from affected cells and putting into email content ie "staff name" is applying for leave on "date"

    xMailBody = "Hi there Priscilla" & vbNewLine & vbNewLine & _
    "Name: " & staff & " is applying for Ad-hoc leave on " & date1 & vbNewLine & vbNewLine & _
    "Reason: " & vbNewLine & vbNewLine & _
    "Thank you" & vbNewLine 'calling out and placing values of each col into email body


    With xMailItem
        .To = "[email protected]"
        .Subject = "Applying for Ad-hoc leave "
        .Body = xMailBody
        .Attachments.Add (ThisWorkbook.FullName)
        .Display
    End With
    Set xRgSel = Nothing
    Set xOutApp = Nothing
    Set xMailItem = Nothing
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End If
End Sub

评论

0赞 shariffa 7/26/2023
嗨,亚历山大,是的,没错,我使用的潜艇是Worksheet_Change。我已经尝试了您提供的代码,但它返回了一条错误消息。编译错误:预期:Then 或 GoTo
0赞 shariffa 7/26/2023
If Target.Interior.Color = RGB (255, 255, 0) 以红色突出显示。@Alexander
0赞 Alexander 7/26/2023
啊,抱歉,你需要在那一行的末尾添加一个 then。我已经编辑了我的答案
0赞 shariffa 7/31/2023
嗨,亚历山大,我已经在上面编辑了我的原始代码。电子邮件已触发,但单元格的颜色是否更改并不重要。只要选择了任何单元格,它就会触发电子邮件,并且电子邮件的正文为空。你能帮忙吗?@Alexander
0赞 Alexander 7/31/2023
@shariffa 在我看来,第二个 If 语句不在第一个语句中,可能会导致电子邮件触发。如果您在此之前没有机会尝试,我稍后会编辑它