VBA to email recipient if column cells contain value

bh24524

Active Member
Joined
Dec 11, 2008
Messages
365
Office Version
  1. 365
  2. 2007
Hello, I have a code I had found a while back that can email recipients. It is as follows:

VBA Code:
Sub savesheet()
Dim Name As String
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    EmailWBAttached
    Name = "\\Sample\" & _
        Format(Now(), "mmddyy") & " " & "Perishable 1ST SHIFT ABSENTEE BLANK (1ST SHIFT)" & ".xlsm"
   
    ActiveSheet.SaveAs Filename:=Name, FileFormat:=52
    Workbooks.Open "\\Sample\1ST SHIFT ABSENTEE REPORT.xlsm"
    ThisWorkbook.Activate
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
   
End Sub

Sub EmailWBAttached()
ActiveWorkbook.Save
Dim OutApp As Object
Dim OutMail As Object

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

On Error Resume Next

    With OutMail
        .Display
        .HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>" & "Attached is the attendance sheet (or revision) to 1st Shift Perishable.<br>" & "</BODY>" & .HTMLBody     
        .To = ActiveSheet.Range("W2").Value      
        .CC = ""
        .BCC = ""
        .Subject = "1st Shift Attendance: " & Format(Now(), "mm.dd.yy") 
       
        .Attachments.Add Application.ActiveWorkbook.FullName
         'or use .Send
    End With

On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

   
End Sub
This code emails a group of people and then saves a special-named copy in a folder. The email list currently doesn't have any CC'd recipients, but I'd like to potentially change that. If this is doable, I'm wondering what alterations could be made to this code to include the CC'd recipients(regular recipients are pulled from cell W1 and CC'd would be pulled from cell W2) if one or more cells in column E contain the phrase "ZZZ-Unexcused Absence". So basically if none of the cells contain that phrase, it just emails the normal listing in the TO field, but if column E does contain that value, then it would email the TO recipients AND the CC recipients. Thanks for your time.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi bh24524,

Try this as the To and CC code for the email:

VBA Code:
If Evaluate("COUNTIF(E:E,""ZZZ-Unexcused Absence"")") >= 1 Then
        .To = Range("W1").Value
        .CC = Range("W2").Value
    Else
        .To = Range("W1").Value
    End If

Regards,

Robert
 
Upvote 0
Solution
Hi bh24524,

Try this as the To and CC code for the email:

VBA Code:
If Evaluate("COUNTIF(E:E,""ZZZ-Unexcused Absence"")") >= 1 Then
        .To = Range("W1").Value
        .CC = Range("W2").Value
    Else
        .To = Range("W1").Value
    End If

Regards,

Robert
Thanks for that I'm going to give this a try when back in the office cuz I do have a question or two.
 
Upvote 0
Thanks again Trebor, it works and it turns out I don't have a question after all. I did some experimenting because I wanted to see if I could get the email to add additional text in the event that additional email address was added and it worked. I underlined the additions I made. The code changed to:

VBA Code:
Sub EmailWBAttached()
ActiveWorkbook.Save
Dim OutApp As Object
Dim OutMail As Object
[U]Dim NCNS As String

NCNS = "HR has been copied on this email because a No Call No Show Entry has been selected"[/U]

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

On Error Resume Next

    With OutMail
        .Display
            
        If Evaluate("Countif(E:E,""ZZZ-No Call No Show"")") >= 1 Then
        .To = ActiveSheet.Range("W2").Value        '<-- enter email addresses here. Multiple emails separate by comma
        .CC = ActiveSheet.Range("W3").Value
        [U].HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>" & "Attached is the attendance sheet (or revision) to 1st Shift Perishable.<br>" & "<br>" & NCNS & "</BODY>" & .HTMLBody '<-- enter message body here[/U]
        Else
        .HTMLBody = "<BODY style=font-size:11pt;font-family:Calibri>" & "Attached is the attendance sheet (or revision) to 1st Shift Perishable.<br>" & "</BODY>" & .HTMLBody
        .To = ActiveSheet.Range("W2").Value
        End If
        .BCC = ""
        .Subject = "1st Shift Attendance: " & Format(Now(), "mm.dd.yy")  '<-- enter subject here
      
        .Attachments.Add Application.ActiveWorkbook.FullName
         'or use .Send
    End With

On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

  
End Sub
So thank you for your help!
 
Upvote 0
I'm glad it all worked out and you're welcome 😎
Okay so it turns out I do have a question and this is just me playing with different scenarios to better understand this coding. So let's say we have E1:E3 as a merged cell and there is a formula in that merged group that will yield the wording "ZZZ-Unexcused Absence" if a certain condition in another cell was met. And we'll say that this is the only place in the entire column E that can have that type of entry. E1 would contain the wording, but it's just merged up to E3. Would the coding be drastically different if what was populated in column E was yielded by a formula and that cell was merged?
 
Upvote 0
Okay so it turns out I do have a question and this is just me playing with different scenarios to better understand this coding. So let's say we have E1:E3 as a merged cell and there is a formula in that merged group that will yield the wording "ZZZ-Unexcused Absence" if a certain condition in another cell was met. And we'll say that this is the only place in the entire column E that can have that type of entry. E1 would contain the wording, but it's just merged up to E3. Would the coding be drastically different if what was populated in column E was yielded by a formula and that cell was merged?
Never mind I answered my own question. It turns out it does not make a difference. Good to know.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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