Generate Email w/Variable Recipients

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I'm trying to code for an email to be sent when there's an overdue action item. So far everything works except for the .To line. Right now, there are 2 possible recipients. Is there a way to run an IF statement to determine the email's recipient based on the value of another cell? Here's what I have now:

VBA Code:
Sub SendIssueEmails()

Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim m As Workbook
Dim mI As Worksheet
Dim mILR As Long
Dim c As Range
Dim Recip As String

Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Set m = ThisWorkbook
Set mI = m.Sheets("Inventory")

mILR = mI.Range("C" & Rows.Count).End(xlUp).Row

For Each c In mI.Range("AC3:AC" & mILR)
    If c.Value > 0 Then
        If c.Offset(0, -11).Value = "RB" Then
            Set Recip = "RB@Company.com" 'I'm getting an Object Required error here.
        ElseIf c.Offset(0, -11).Value = "MM" Then
            Set Recip = "MM@Company.com"
        End If
        
        With OutlookMail
            .To = Recip
            .CC = "RB@Company.com"
            .Subject = c.Offset(0, -26)
            .Body = "The subject initiative has at least 1 overdue action item.  Please review the action item and respond with justification for its delinquency."
            .Display
        End With
    End If
Next c
            
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You don't Set string variables you assign values to them. You only Set object variables. You already seem to have the IF block in play, so I'm not sure if your question is about whether or not you can use one, or if it's how to fix what you have. Try
Recip = "RB@Company.com" and Recip = "MM@Company.com"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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