Using VBA to email to a list of recipients of varying length

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
I have this sendmail code I have used in the past to email to a list of recipients that can change in length. I have made some modifications to the code however never touched the "To: cad" part of it
It no longer works and I don't know what I did wrong.

The email list will be 1 to 5 people most likely.

Any help with this section of the code would be greatly appreciated.

VBA Code:
Sub sendmail()
  Dim OutlookApp As Object, MItem As Object, cad As String
  Dim i As Long, Sh As Worksheet, rng As Range, lr As Long
 
  Set Sh = Sheets("Warranty Email")
  lr = Sh.Range("A" & Rows.Count).End(xlUp).Row
  Set rng = Sh.Range("A1:R" & lr)
'here
For i = 2 To lr
    cad = cad & Sh.Range("V" & i).Value & "; "
  Next
  Set OutlookApp = CreateObject("Outlook.Application")
  Set MItem = OutlookApp.CreateItem(0)
  With MItem
'and here
    .To = cad
    .Subject = Sh.Range("W1").Value
    .htmlBody = Sh.Range("W2").Value & RangetoHTML(rng) & _
      "<br>Thank you<br>"
.Display
    .Send
  End With
End Sub

Customer Concern - Warranty Request Log.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1Customer Concern NumberDateLatest W/O #Previous W/O #(s)PO#Model TypeCustomer NameP/NS/NDescriptionDiscrepancyTotal number of PartsArea Issued toVerification completed Y/NWarranty Granted Y/NWarranty CostWork done in Good Faith Y/NAdditional NotesInspector List:carla.fisher@email.comPending Warranty Inspections/Required Updates
2CC330625-Oct-23TBDN/A1234N/ACustomer201604N/APartsdamage2AreaNTBDThe below listed Warranties require inspection and/or updates. Please complete the items in yellow and email back to the QA Dept.
Warranty Email
 

Attachments

  • Error.JPG
    Error.JPG
    19.3 KB · Views: 5

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Just by looking at your code, I'd suggest moving the "Next" line after the "End With" line.
 
Upvote 0
A closer look at your code indicates that the Next was in the right spot. Sorry!

What error are you getting? I don't have Outlook installed on my PC, so I can't actually test your code. Maybe someone else can jump in.
 
Upvote 0
You're concatenating a list of email recipients from Column V, starting at Row 2. But your sample worksheet shows that there's no value in V2. And maybe you don't have any other emails listed in the rest of the column. So, as the error message says, you need at least one name.
 
Upvote 0
Solution
You're concatenating a list of email recipients from Column V, starting at Row 2. But your sample worksheet shows that there's no value in V2. And maybe you don't have any other emails listed in the rest of the column. So, as the error message says, you need at least one name.
That's what I was missing! Thank you so much!

For i = 2 should be For i = 1

Thank you very much for the second set of eyes, or three!

Thanks to both of you :)
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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