Excel VBA如何在电子邮件正文的更改中包含单元格内容

Excel VBA如何在电子邮件正文的更改中包含单元格内容,excel,vba,email,outlook,Excel,Vba,Email,Outlook,大家好,代码编写者和知识寻求者。我有一个代码,它正在发送一封电子邮件,通知J列(提交日期)中的单元格已添加日期。我只想在B列(提交文件标题)中包含与已添加日期对应的单元格内容 代码运行良好,当我在J列的单元格中添加日期时发送电子邮件。但我想在电子邮件正文中添加提交文件标题。这是我的密码 Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range Dim SubmitLink As Ran

大家好,代码编写者和知识寻求者。我有一个代码,它正在发送一封电子邮件,通知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
然后确定此单元格与关键单元格的偏移量

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

令人困惑的描述。总之,只要查看这一行并进行相应的更改(newmsg.Body=“亲爱的用户,新提交文件”和单元格(SubmitLink.Row,“B”)&“已添加到Musanda ICA在线日志中。请调查更改”),首先,感谢您的回复。你是对的。我将重新措辞。我只希望我的代码在我添加新提交日期时发送一封电子邮件。在这封邮件的正文中,我想添加此提交文件的标题。第J列是我输入提交日期的地方,第B列是我输入提交标题的地方。您能否添加电子表格外观的快照以及您希望对正文进行哪些更改?我有点明白你的意思,但不完全明白。谢谢你的回复。我得到了解决方案,我会将其发布给其他人也会受益:)