简体   繁体   中英

Looking for Outlook to send an email based from data generated from an IF statement

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

Briefing

On VBA there're several ways to access a Sheet and the most common are through:

  • The sheet name , which you can set on the Excel Sheet (and the user can change this)

在此输入图像描述

And then call in your code like this:

Dim myWorksheet As Worksheet   
Set myWorksheet = Worksheets("Sheet1") 'The user defined Excel sheet name.

  • The Microsoft Excel Object sheet (which you can find on the left pane)

在此输入图像描述

And you can simply call it like this (assuming its name is Sheet1 ):

Sheet1.Activate

Your code

So, going back to your code, since the variable MasterCheck is not initialized , we can use the first method and initialize its value:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim sEmailBodyp1 As String
    Dim sEmailSubject As String
    Dim sEmailTo As String
    Dim Outlook As Object
    Dim MasterCheck As Worksheet

    Set MasterCheck = Sheets("MySheet") 'Change "MySheet" with your sheet name

    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

Something like this should work:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B:B")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub

    Dim sEmailBodyp1 As String
    Dim sEmailSubject As String
    Dim sEmailTo As String
    Dim Outlook As Object
    Dim MasterCheck As Worksheet

    Set MasterCheck = ActiveWorkbook.Sheets("SheetName") 'Change this with your sheet name

    sEmailTo = MasterCheck.Range("D2").Value
    sEmailSubject = MasterCheck.Range("E2").Value
    sEmailBodyp1 = MasterCheck.Range("F2").Value

    If Target.Value = "Promo Today" Then
        With CreateObject("Outlook.Application").CreateItem(0)
            .To = sEmailTo
            .Subject = sEmailSubject
            .Body = sEmailBodyp1
            .Send
        End With
    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM