VBA code needed to extract cell info from a table and insert a variety of information into the body of an email

MickyT7

New Member
Joined
Aug 12, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Its been a while since I've posted anything so apologies if my upfront info is lacking (happy to add more if requested), but I'll try my best to describe the challenge I've got.

Objective - Auto send an email with various info points from excel when a target date is reached in a specific row.

Summary - I've set a trigger date using the formula =TODAY() to always show todays date and is activated when another cell in that row matches the value. I've also got cells around this date assigned to work tasks which will, at some point, match the date in the =TODAY() formula, and then an IF(task_date=TODAY TRIGGER, "EMAIL",0) will appear in a corresponding cell row of that tasks line. i.e task 1 is set for 6 weeks from today, and when the date eventually matches the =TODAY() formula date in 6 weeks time, it will then activate the IF statement and this is what I'd like to trigger the VBA code to send an email to me.

So in an ideal world, column B is the manual input date (delivered date) field that will trigger all cells in that row, and if the task is located in column A, the task due date is column F, the =TODAY() date trigger is in column G and the IF statement "EMAIL" trigger is in column H. Once the task due date matches the =TODAY() formula, it activates the IF statement and triggers the email into a send/ display option with the appropriate information that corresponds to that row from the desired col/ row locations and insert this info into the body of the email as outlined in the code and ref points of the sheet below.

The below VBA code is what I have so far and I'm struggling with the range setup and how to get the cell values into the body of the email. Thanks in advance, and please ask for anymore info if required. I've attached a pic of the sheet and the VBA code for visual representation.

Dim xRg As Range
'Update by Extendoffice 2018/3/7
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count = 0 Then Exit Sub
Set xRg = Intersect(Range("U2:U200000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value = "EMAIL" Then
Call Mail_small_Text_Outlook
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String

Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)

'THE BELOW IS THE EMAIL BODY I'D LIKE POPULATED WITH CELL INFORMATION THAT CORRESPONDS TO THE ROW THAT HAS BEEN ACTIVATED BY THE FORMULAS
xMailBody = "Good day," & vbNewLine & vbNewLine & _
"Upon reaching its validation due date, (task_name) is now ready for validation in week (validation_week)." & vbNewLine & vbNewLine & _
"Please arrange a slide and business case to showcase the benefits" & vbNewLine & vbNewLine & _
"Kind Regards,"

On Error Resume Next
With xOutMail
.To = "email1@email"
.CC = ""
.BCC = ""
.Subject = "Validation items are due (TEST PLEASE IGNORE)"
.Body = xMailBody
'.Attachments.Add ActiveWorkbook.FullName
.Display
'or use
'.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Private Sub Workbook_Open()
Sheets("Sheet12").Range("U2:U200000").Value = "EMAIL"
End Sub
 

Attachments

  • vba_sheet_ref.PNG
    vba_sheet_ref.PNG
    15.8 KB · Views: 15

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top