VBA Macro to send email based on cell value

cantona_007

New Member
Joined
Aug 1, 2012
Messages
19
Hello All,

I have a vba code like the one below that currently works (Sorry, I can't share because it contains confidential information). I have a macro that sends the data in various cells in the excel sheet as an html email when a cell (A1:A100) contains the word "Completed", but what I'm looking for now is a way to include another macro that generates a different email when A1:A100= "Incomplete". I'm not that vba savvy, so I hope I make a little sense. I think it should look something along the lines of:

If (A1:A100) = "Completed" , Send (name of Macro containing html body template)
else if (A1:A100) = "Incomplete", Send (name of Macro containing a different html body template)

But I'm pretty I am wrong.


All help is much appreciated.

Thanks so much!


Sub Mail_Selection_Range_Outlook_Body()'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
'Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)

On Error GoTo 0

If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

With Application
.EnableEvents = False
.ScreenUpdating = False
End With

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

On Error Resume Next
With OutMail
.To = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.HTMLBody = RangetoHTML(rng)
.Send 'or use .Display
End With
On Error GoTo 0

With Application
.EnableEvents = True
.ScreenUpdating = True
End With

Set OutMail = Nothing
Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
Dim fso As Object

Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With

'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With

'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")

'Close TempWB
TempWB.Close savechanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
when a cell (A1:A100) contains the word "Completed", but what I'm looking for now is a way to include another macro that generates a different email when A1:A100= "Incomplete".
Try this:
Code:
Public Sub Search_Cells_Send_Email()

    Dim findText As Variant
    
    findText = Application.Match("Completed", Worksheets("Sheet1").Range("A1:A100"), 0)
    If Not IsError(findText) Then
        'Found "Completed"
        Send_1st_Email
    Else
        findText = Application.Match("Incomplete", Worksheets("Sheet1").Range("A1:A100"), 0)
        If Not IsError(findText) Then
            'Found "Incomplete"
            Send_2nd_Email
        Else
            MsgBox "Neither ""Complete"" nor ""Incomplete"" found in A1:A100"
        End If
    End If
End Sub

Private Sub Send_1st_Email()
    MsgBox "Completed found - Send_1st_Email"
End Sub

Private Sub Send_2nd_Email()
    MsgBox "Incomplete found - Send_2nd_Email"
End Sub
 
Upvote 0
Re: If Else statement in a VBA Module |VBA Macro to send email based on cell value |

That didn't quite work for me.. perhaps I missed a step or something.

But here's what I was able to come up with.

I have two different modules for "Complete” and "Incomplete"


"Complete”

------------------------------------------------------

Sub Worksheet()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup
For Each cell In Columns("K").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "AO").Value) = "yes" Then


Set OutMail = OutApp.CreateItem(0)



Strbody1 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P><br />"
Strbody2 = "<P STYLE='font-family:Calibri;font-size:14'><font color=red><b>Confidential</P>"
strbody3 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
strbody4 = "<P STYLE='font-family:Calibri;font-size:14'><u>Confidential</p>"
Strbody5 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody6 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody7 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody8 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
strbody9 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody10 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody11 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody12 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P><br />"
strbody13 = "<P STYLE='font-family:Calibri;font-size:14'><b><u>Confidential</b></u></P>"
Strbody14 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
strbody15 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</A> </P>"
Strbody16 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</A> </P>"
Strbody17 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</A> </P>"
Strbody18 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</A> </P>"
strbody19 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</b></u></P>"
Strbody20 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody21 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody22 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
strbody23 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"

On Error Resume Next
With OutMail
.SentOnBehalfOfName = "name@mail.com"
.To = cell.Value
.Subject = Cells(cell.Row, "B").Value & Space(1) & "(" & Cells(cell.Row, "F").Value & Space(1) & Cells(cell.Row, "G").Value & ")" & Space(1) & "**DO NOT REPLY TO THIS E-MAIL**"
.HTMLBody = Strbody1 + Strbody2 + strbody3 + strbody4 + Strbody5 + Strbody6 + Strbody7 + Strbody8 + strbody9 + Strbody10 + Strbody11 + Strbody12 + strbody13 + Strbody14 + strbody15 + Strbody16 + Strbody17 + Strbody18 + strbody19 + Strbody20 + Strbody21 + Strbody22 + strbody23


.Send 'Or use Display
End With
On Error GoTo 0
Set OutMail = Nothing
End If
End If
Next cell

cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
------------------------------------------------------------------------


------------------------------------------------------------------------

"Incomplete"

Sub Worksheet()
Dim OutApp2 As Object
Dim OutMail2 As Object
Dim cell As Range

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup
For Each cell In Columns("K").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "AO").Value) = "yes" Then

Set OutMail2 = OutApp.CreateItem(0)

Strbody1 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P><br />"
Strbody2 = "<P STYLE='font-family:Calibri;font-size:14'><b><u>Reject Reason:</b></u></P>"
strbody3 = "<P STYLE='font-family:Calibri;font-size:14'><Confidential</b></font></P>"
strbody4 = "<P STYLE='font-family:Calibri;font-size:14'><b><u>Comments:</b></u></P>"
Strbody5 = "<P STYLE='font-family:Calibri;font-size:14'><font color=red><b> Confidential</b></font></P>"
Strbody6 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential.</P>"
Strbody7 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody8 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P><br />"
strbody9 = "<P STYLE='font-family:Calibri;font-size:14'><b><u>Confidential</b></u></P>"
Strbody10 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody11 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody12 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
strbody13 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</A> </P>"
Strbody14 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
strbody15 = "<P STYLE='font-family:Calibri;font-size:14'><b><u>Confidential</b></u></P>"
Strbody16 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody17 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
Strbody18 = "<P STYLE='font-family:Calibri;font-size:14'>Confidential</P>"
strbody19 = "<P STYLE='font-family:Calibri;font-size:14'>-Confidential</P>"

On Error Resume Next
With OutMail2
.SentOnBehalfOfName = "name@mail.com"
.To = cell.Value
.Subject = Cells(cell.Row, "B").Value & Space(1) & "(" & Cells(cell.Row, "F").Value & Space(1) & Cells(cell.Row, "G").Value & ")" & Space(1) & "**DO NOT REPLY TO THIS E-MAIL**"
.HTMLBody = Strbody1 + Strbody2 + strbody3 + strbody4 + Strbody5 + Strbody6 + Strbody7 + Strbody8 + strbody9 + Strbody10 + Strbody11 + Strbody12 + strbody13 + Strbody14 + strbody15 + Strbody16 + Strbody17 + Strbody18 + strbody19


.Send 'Or use Display
End With
On Error GoTo 0
Set OutMail2 = Nothing
End If
Next cell

cleanup:
Set OutApp2 = Nothing
Application.ScreenUpdating = True
End Sub


--------------------------------------------------------------

-------------------------------------------------------------

They both work fine, but I'm struggling with inserting an 'If Else' statement to say if the values in (A1:A100) = "Complete" send an email using module 1, else if the values in (A1:A100) = "Incomplete" send the email using module 2.


Any help is much appreciated.

Thank you!
 
Upvote 0
Re: If Else statement in a VBA Module |VBA Macro to send email based on cell value |

Sorry for bumping again, and sorry for sending the super long previous post.

Thanks in advance!
 
Upvote 0
Re: If Else statement in a VBA Module |VBA Macro to send email based on cell value |

so sorry to keep bumping this, but I am a little desperate.

thanks all!
 
Upvote 0
Re: If Else statement in a VBA Module |VBA Macro to send email based on cell value |

Send_1st_Email and Send_2nd_Email were just stubs for your own code.

Try this:
Code:
Public Sub Search_Cells_Send_Email()

    Dim findText As Variant
    
    findText = Application.Match("Completed", ActiveSheet.Range("A1:A100"), 0)
    If Not IsError(findText) Then
        'Found "Completed"
        Module1.Worksheet
    Else
        findText = Application.Match("Incomplete", ActiveSheet.Range("A1:A100"), 0)
        If Not IsError(findText) Then
            'Found "Incomplete"
            Module2.Worksheet
        Else
            MsgBox "Neither ""Complete"" nor ""Incomplete"" found in A1:A100"
        End If
    End If
End Sub
Your code doesn't have any worksheet references, so it defaults to the active sheet. The above code also uses the active sheet, therefore ensure that your data sheet is active before you run the above routine.
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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