簡體   English   中英

Excel VBA如何在電子郵件正文更改時包含單元格內容

[英]Excel VBA how to include cell content on change in email body

你好編碼員和知識尋求者。 我有這個代碼發送一封電子郵件,通知日期已添加到 J 列(提交日期)的單元格中。 我只想在與已添加日期相對應的 B 列(提交標題)中包含單元格的內容。

當我在 J 列的單元格中添加日期時,代碼工作正常並發送電子郵件。但我想在電子郵件正文中添加該提交標題。 這是我的代碼

  Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range
    Dim SubmitLink As Range

    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("J3:J1000")
    Set SubmitLink = Range("B3:B1000")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
       Is Nothing Then

        ' Display a message when one of the designated cells has been
        ' changed.
        ' Place your code here.
        Dim answer As String

        answer = MsgBox("Do you wish to save this change. An Email will be sent to the User", vbYesNo, "Save the change")

        If answer = vbNo Then Cancel = True
        If answer = vbYes Then
            'open outlook type stuff
            Set OutlookApp = CreateObject("Outlook.Application")
            Set OlObjects = OutlookApp.GetNamespace("MAPI")
            Set newmsg = OutlookApp.CreateItem(olMailItem)
            'add recipients
            'newmsg.Recipients.Add ("Name Here")
            newmsg.Recipients.Add Worksheets("Coordinator").Range("Q4").Value
            'add subject
            newmsg.Subject = Worksheets("Coordinator").Range("O3").Value
            'add body
            newmsg.Body = "Dear User, New Submittal" & Cells(SubmitLink.Row, "B") & "has been Added in SUBMITTAL Log. Please Investigate the Change"
            newmsg.Display    'display
            newmsg.Send    'send message
            'give conformation of sent message
            MsgBox "Modification confirmed", , "Confirmation"



        End If
        '     MsgBox "Cell " & Target.Address & " has changed."

    End If
End Sub

感謝您的幫助

這就是解決方案。 只需定義要從中檢索數據的單元格

Dim SubmitLink As String

然后從 KeyCell 中確定該單元格的偏移量

SubmitLink = Target.Offset(, -8).Value

最后在電子郵件正文或標題的文本中添加 ( " & SubmitLink & " ),該單元格中的數據將出現。 這是完整的代碼

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range


    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set KeyCells = Range("J3:J1000")


    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        ' Display a message when one of the designated cells has been
        ' changed.
        ' Place your code here.
Dim answer As String
Dim SubmitLink As String

SubmitLink = Target.Offset(, -8).Value

answer = MsgBox("Do you wish to save this change. An Email will be sent to the User", vbYesNo, "Save the change")

If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook type stuff
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
'newmsg.Recipients.Add ("Name Here")
newmsg.Recipients.Add Worksheets("Coordinator").Range("Q4").Value
'add subject
newmsg.Subject = Worksheets("Coordinator").Range("O3").Value
'add body
newmsg.Body = "Dear User, New Submittal ( " & SubmitLink & " ) has been Added in Submittal Log. Please Investigate the Change" & vbLf & vbLf & vbLf & "Sincerely," & vbLf & "logs department"

newmsg.Display 'display
newmsg.Send 'send message
'give conformation of sent message
MsgBox "Modification confirmed", , "Confirmation"



End If
   '     MsgBox "Cell " & Target.Address & " has changed."

End If
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM