Can you please write me two VBA codes that will do the following:
When I change the value in column M from row 2 downwards
pop up a MsgBox "To email the recipiant" line sapce "double click on the Outcome", vbInformation, "EMAIL"
When I double click on a cell in column K from row 2 downwards,
check if it has a comment. If it does, pop up a Yes No message
"Do you want to Resend" line break And the contents of the Comment.
If Yes, clear the current comment and continue
If No, Exit the sub.
On the row that I have clicked on gather the data in this arrangement:
"TASK: " & Column A value line space
"EXECUTION: " & Column I value line space
"OUTCOME: " & Column K value line space
"DATE COMPLETED: " & Date Value in column C + number of days in column L
then open a new Outlook Email
Paste the value gathered as the Body of the email
with Subject as "Task Status"
and To: value in column M
Then add a comment in Target column K
That reads " Task Status sent to: " & Value in column M before the @ in the value & Date
Assistant
Certainly! Below are the two VBA codes you can use for the functionality you've described.
Code 1: Detect Change in Column M
This code will trigger a message box when the value in column M (rows 2 and downwards) changes.
vba
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
If Not Intersect(Target, Me.Columns("M")) Is Nothing Then
For Each Cell In Intersect(Target, Me.Columns("M"))
If Cell.Row >= 2 Then
MsgBox "To email the recipient" & vbCrLf & "double click on the Outcome", vbInformation, "EMAIL"
End If
Next Cell
End If
End Sub
Code 2: Handle Double Click in Column K
This code will handle double-click events in column K (rows 2 and downwards) to check for comments and create an email if needed.
vba
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim msgResponse As VbMsgBoxResult
Dim emailBody As String
Dim recipient As String
Dim taskComment As String
Dim taskValue As String
Dim dateCompleted As Date
If Not Intersect(Target, Me.Columns("K")) Is Nothing Then
If Target.Row >= 2 Then
If Not Target.Comment Is Nothing Then
taskComment = Target.Comment.Text
msgResponse = MsgBox("Do you want to Resend?" & vbCrLf & taskComment, vbYesNo + vbQuestion, "Resend Confirmation")
If msgResponse = vbNo Then Exit Sub
Target.Comment.Delete
End If
' Gather data for the email body
taskValue = "TASK: " & Me.Cells(Target.Row, "A").Value & vbCrLf & _
"EXECUTION: " & Me.Cells(Target.Row, "I").Value & vbCrLf & _
"OUTCOME: " & Target.Value & vbCrLf & _
"DATE COMPLETED: " & Format(Me.Cells(Target.Row, "C").Value + Me.Cells(Target.Row, "L").Value, "mm/dd/yyyy")
' Get recipient's email address
recipient = Me.Cells(Target.Row, "M").Value
' Create and open a new Outlook email
Dim OutlookApp As Object
Dim OutlookMail As Object
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
With OutlookMail
.To = recipient
.Subject = "Task Status"
.Body = taskValue
.Display ' Change to .Send if you want to send directly
End With
' Add a comment in Target column K
Target.AddComment "Task Status sent to: " & Split(recipient, "@")(0) & " " & Format(Date, "mm/dd/yyyy")
End If
End If
End Sub
How to Use the Code
Open Excel.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor.
In the Project Explorer window, find the sheet where you want to add this functionality (e.g., "Sheet1").
Double-click on that sheet to open its code window.
Copy and paste the two code snippets provided above into that window.
Close the VBA editor and return to your Excel sheet.
Now, the Excel sheet will trigger the specified actions when you change values in column M and when double-clicking cells in column K. Remember to enable macros in your Excel file and check your Outlook settings to ensure that emails can be sent from VBA.