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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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