Automatically send email based on cell text, multiple variables

Angela J

New Member
Joined
Aug 3, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello and thanks for helping. I am very new to VBA. I have a spreadsheet that needs to be emailed to different reviewers at different intervals based on the text in the cell. For example, if column O = "Y" then send email to userA@outlook.com; if column P = "Y" then send to userB@outlook.com; if column Q = "Y" then send to userC@outlook.com. So far I have the following, but don't know how to add the other variables without getting errors.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address = "O2:O26" And Target.Value <> "" Then
        CreateMail
    End If
End Sub

Sub CreateMail()
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "userA@outlook.com"
        .CC = ""
        .BCC = ""
        .Subject = "New document ready for review"
        .Body = "Hi, There is a new document ready for you to review."
        .Attachments.Add ("C:\")
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Last edited by a moderator:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hello and thanks for helping. I am very new to VBA. I have a spreadsheet that needs to be emailed to different reviewers at different intervals based on the text in the cell. For example, if column O = "Y" then send email to userA@outlook.com; if column P = "Y" then send to userB@outlook.com; if column Q = "Y" then send to userC@outlook.com. So far I have the following, but don't know how to add the other variables without getting errors.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Target.Address = "O2:O26" And Target.Value <> "" Then
        CreateMail
    End If
End Sub

Sub CreateMail()
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "userA@outlook.com"
        .CC = ""
        .BCC = ""
        .Subject = "New document ready for review"
        .Body = "Hi, There is a new document ready for you to review."
        .Attachments.Add ("C:\")
        .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
I figured out my mistake. No help needed with this one anymore.
 
Upvote 0
I figured out my mistake. No help needed with this one anymore.
My solution was that I did not end each of my if statements. BUT, I still have lots of questions regarding how to get this workbook to do what I want it to do. I have a multi sheet workbook, a sheet for each month of the year. I need to apply the macro to all sheets in the workbook. Ideally, I would like an email generated when a specific cell is populated AND the document is saved. There are 3 variables, based on which cell is populated, a different user is to be emailed. Do I apply this to ThisWorkbook under VBAobjects? I feel like I am missing a lot of steps needed to give me my desired outcome.
 
Upvote 0
Still looking for some help, maybe more clarification of what I need help with is needed. I am looking to have an email automatically sent to one of three recipients, dependent on which column is checked on the spreadsheet. I would like the email automatically sent after save. The code I have so far will display the email when prompted but will not send it automatically as I would like it to. This is what I have:

Private Sub Workbook_AfterSave(ByVal Success As Boolean)

If Target.Cells.Count > 1 Then Exit Sub
If Target.Address = "O2:O26" And Target.Value <> "" Then
CreateMail
End If
If Target.Address = "P2:P26" And Target.Value <> "" Then
CreateMail2
End If
If Target.Address = "Q2:Q26" And Target.Value <> "" Then
CreateMail3
End If


Sub CreateMail()
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "UserA@outlook.com"
.CC = "userD@outlook.com"
.BCC = ""
.Subject = "New document ready for review"
.Body = "Hi UserA, There is a new document ready for you to review."
.Attachments.Add ("G:\Spreadsheet.xlsm")
.Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Sub CreateMail2()
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "UserC@outlook.com"
.CC = ""
.BCC = ""
.Subject = "Document ready for review/approval"
.Body = "Hi UserC, There is a new document ready for you to review."
.Attachments.Add ("G:\Spreadsheet.xlsm")
.Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Sub CreateMail3()
Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "userB@outlook.com"
.CC = ""
.BCC = ""
.Subject = "A document has been approved"
.Body = "Hi userB, A document has been approved and ready for you to follow up with."
.Attachments.Add ("G:\Spreadsheet.xlsm")
.Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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