VBA to create email using values from different columns

mpatino

Board Regular
Joined
Jul 8, 2009
Messages
82
Hi Gurus,

I need a way to create emails based on cell value, for example in Column T, I will have "Yes" / "No" on each row, so, if the row has "Yes", then, that row should generate an email with values from the same row, but different columns, for example:

To: Data from column B
Subject: Data from column D & A
Body:
Salutation + Data from Column M + signature

This should generate as many emails as "Yes's" are in the worksheet Column T

Appreciate any assistance provided.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this;

Code:
Sub EmailStuff()
Dim OutApp As Object
Dim OutMail As Object
Dim strBody As String
Dim strTo As String
Dim strSub As String
Dim LastRow As Integer

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

LastRow = Range("T" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

    If Cells(i, "T").Value = "Yes" Then
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)

        strTo = Cells(i, "B").Value
        
        strSub = Cells(i, "D").Value & " " & Cells(i, "A").Value
        
        strBody = "Good Morning" & vbNewLine & vbNewLine
        strBody = strBody & Cells(i, "M").Value & vbNewLine & vbNewLine
        strBody = strBody & "Regards" & vbNewLine & vbNewLine & "JazzSP8"
        
        On Error Resume Next
        With OutMail
            .to = strTo
            .Subject = strSub
            .Body = strBody
            .Display   ' Change to .Send to automatically send
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing

    End If

Next i

Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 
Upvote 0
Hi JazzSP8,

Thank you so very much, this works like charm, I really appreciate you taking the time to help me and this community.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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