I'm aware there are other posts about the same thing, but I can't get it to work with my variables.
I have 2 columns of data that use IF statements, these statements are IF(Asus!C:C=TODAY(),"Promo Today","")
. I want to use VBA to detect when a cell turns into "Promo Today"
and send out an email to the recipients (from my understanding, the email address had to be defined in a cell, as seen in the code I'll post, I tried to get that into it).
Columns 2 and 3, as I've tried to target in the code, are the columns in which the IF statements are located, something I didn't take into account and am only just thinking of is will I have to target a row as well?
Private Sub Worksheet_Change()
Dim sEmailBodyp1 As String
Dim sEmailSubject As String
Dim sEmailTo As String
Dim Outlook As Object
Dim MasterCheck As Worksheet
sEmailTo = MasterCheck.Range("D2").Value
sEmailSubject = MasterCheck.Range("E2").Value
sEmailBodyp1 = MasterCheck.Range("F2").Value
If Target.Column = 2 And Target.Value = "Promo Today" Then
With CreateObject("Outlook.Application").CreateItem(0)
.To = sEmailTo
.Subject = sEmailSubject
.Body = sEmailBodyp1
.Send
End With
End If
End Sub
Target
, that you want VBA to either loop through the Excel data and send an email for each occurrence of Promo Today, or send one email if Promo Today occurs in any cell in Column 2, both of which alleviate you having to target the cell in question. Is that correct? Note thatTarget
is the current selection, soTarget.Row
andTarget.Column
will give the coordinates of the selected cell. – Cyril Apr 18 '19 at 14:23