Hello fellow coders and knowledge seekers. I have this code that is sending an email informing that a date has been added to a cell in J column (Submittal Date). I just want to include the content of a cell in column B (Submittal title) corresponding to that date that has been added.
The code is working fine and sending an email when I add a date in a cell in column J. But I want to add that submittal title in the body of the email. Here is my code
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
Thank you for your help
This is the solution. Just define the cell you want to retrieve the data from
Dim SubmitLink As String
Then identify this cell's offset from the KeyCell
SubmitLink = Target.Offset(, -8).Value
And finally add ( " & SubmitLink & " ) in the text of the email body or title, and the data in that cell will appear. And here is the full code
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.