Send E-mail from cell values using VBA

crzytimes

New Member
Joined
Oct 28, 2016
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hey!

I'm looking for some assistance in getting me in the right direction. I would like to automate e-mailing multiple vendors from a spreadsheet I would export from SAP and touch up. It would look something like this table. I want to get the rows for each vendor into the email body addressed just to that vendor, without sending multiple e-mails to the same vendor.

Is this type of automation possible with VBA code?



MaterialDescriptionQTYVendorRelease DateContactE-mail Address
partnumberAscrew A100vendor A5/15/2017JonvendorA@vendorA.com
partnumberBscrew B100vendor B5/15/2017RandyvendorB@vendorB.com
partnumberCscrew C100vendor C5/17/2017BobvendorC@vendorC.com
partnumberCCscrew D100vendor C5/17/2017BobvendorC@vendorC.com
partnumberCCCscrew E100vendor C5/17/2017BobvendorC@vendorC.com
partnumberDscrew F100vendor D5/15/2017SarahvendorD@vendorD.com
partnumberEscrew G100vendor E5/16/2017ClaravendorE@vendorE.com
partnumberEscrew H100vendor E5/16/2017ClaravendorE@vendorE.com

<tbody>
</tbody>
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi,

Modified my code and created a table for the mail body: It works with your example table.

HTML:
Sub Send_Table()

'Set email address as range for first loop to run down
 Set rng = Range(Range("G2"), Range("G" & Rows.Count).End(xlUp))
          
'Get a row count to clear column H at the end
  x = rng.Rows.Count
 
'Create the html table and header from the first row
    tableHdr = "<table border=1><tr><th>" & Range("A1").Value & "</th>" _
            & "<th>" & Range("B1").Value & "</th>" _
            & "<th>" & Range("C1").Value & "</th>" _
            & "<th>" & Range("D1").Value & "</th>" _
            & "<th>" & Range("E1").Value & "</th>" _
            & "<th>" & Range("F1").Value & "</th>" _

'Check to see if column H = 'yes' and skip mail if it does
    For Each cell In rng
    If cell.Value <> "" Then
    If Not cell.Offset(0, 1).Value = "yes" Then

        
    NmeRow = cell.Row

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

    MailTo = cell.Value 'column G
    MailSubject = cell.Offset(0, -3).Value 'column D
    
'Create MailBody table row for first row
    MailBody = "<tr>" _
            & "<td>" & cell.Offset(0, -6).Value & "</td>" _
            & "<td>" & cell.Offset(0, -5).Value & "</td>" _
            & "<td>" & cell.Offset(0, -4).Value & "</td>" _
            & "<td>" & cell.Offset(0, -3).Value & "</td>" _
            & "<td>" & cell.Offset(0, -2).Value & "</td>" _
            & "<td>" & cell.Offset(0, -1).Value & "</td>" _
            & "</tr>"

'Second loop checks the email addresses of all cells following the current cell in the first loop.
'Yes will be appended on any duplicate finds and another row added to the mailbody table
    For Each dwn In rng.Offset(NmeRow - 1, 0)


 
    If dwn.Value = cell.Value Then

'Create additional table row for each extra row found
    AddRow = "<tr>" _
            & "<td>" & dwn.Offset(0, -6).Value & "</td>" _
            & "<td>" & dwn.Offset(0, -5).Value & "</td>" _
            & "<td>" & dwn.Offset(0, -4).Value & "</td>" _
            & "<td>" & dwn.Offset(0, -3).Value & "</td>" _
            & "<td>" & dwn.Offset(0, -2).Value & "</td>" _
            & "<td>" & dwn.Offset(0, -1).Value & "</td>" _
            & "</tr>"

    dwn.Offset(0, 1).Value = "yes"
    MailBody = MailBody & AddRow  'column A

    End If
' Clear additional table row variable ready for next
    AddRow = ""
    Next
        With OutMail
            .To = MailTo
            .Subject = MailSubject
            .HTMLBody = tableHdr & MailBody & "</table>"
            .Display
        'send
       End With
        
    cell.Offset(0, 1).Value = "yes"
  
  End If
 End If
 
 
MailTo = ""
MailSubject = ""
MailBody = ""
Next

'Clear 'yes' from all appended cells in column H
 Range("H2:H" & x).ClearContents
End Sub
 
Last edited:
Upvote 0
Thank you :) You've done too much! I'll be able to adjust from here.

This is awesome!
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,841
Members
449,471
Latest member
lachbee

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