I have written some code based on your posted workbook example - if the workbook is set up this way then the following code should work. It will calculate what data to attach to each email based on how your example workbook it setup. I have set up the code to Display the email - all you need to do is comment this out and uncomment the .send - if you have code set to send then the email will be sent automatically so I will leave this to you to decide - it is commented in the code as follows. I have attached an example workbook with the code so make sure it works to your specifications. I would recommend when you use this on real data that you leave the code to Display only just until the code has been tested to your satisfaction that the correct data is being placed into the email. You can change the various attributes such as the signature and leading comment in the body of the email - this again has been commented in the code so modify it to your needs. If you need any help then post back.
I have placed a button on the worksheet to run the code. There is error handling in the code so if there are any errors then please make sure you record the entire error when posting back or do a screen dump.
Option Explicit
Const bodyString As String = "Please advised the following accounts."
Const signStr As String = "John Doe" & vbCrLf & "ABC Company" & vbCrLf & "Tel:123-456-7890"
'change the First string above to alter the leading message in the email
'change the Second string above to change the signature in the email
Dim dataHdrs As String
Sub writeEmails()
Dim OutApp As Object
Dim OutMail As Object
Dim sndToList, ccToList, bccList, sList As String, cList As String, bList As String
Dim subjectStr As String, k As Long, x As Long, bodyStr As String
Dim dataBody
On Error GoTo ErrHandler
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
'determine data blocks
ReDim dlMarkers(x)
For k = 2 To Cells(Rows.Count, "A").End(xlUp).Row
If Range("E" & k) <> "" Then
ReDim Preserve dlMarkers(x)
dlMarkers(x) = k
x = x + 1
End If
Next
ReDim Preserve dlMarkers(x)
dlMarkers(x) = k
x = 0
dataHdrs = Range("A1") & Chr(9) & Chr(9) & Range("B1") & Chr(9) & Range("C1") & Chr(9) & Range("D1")
For k = 2 To Cells(Rows.Count, "A").End(xlUp).Row
sndToList = Application.Index(Application.Transpose(Range("F" & dlMarkers(x) & ":J" & dlMarkers(x))), 0, 1)
ccToList = Application.Index(Application.Transpose(Range("K" & dlMarkers(x) & ":O" & dlMarkers(x))), 0, 1)
bccList = Application.Index(Application.Transpose(Range("P" & dlMarkers(x) & ":T" & dlMarkers(x))), 0, 1)
dataBody = Range("A" & dlMarkers(x)).Resize(dlMarkers(x + 1) - dlMarkers(x), 4)
subjectStr = Range("E" & dlMarkers(x))
sList = EmailList(sndToList)
cList = EmailList(ccToList)
bList = EmailList(bccList)
bodyStr = BodyList(dataBody)
On Error Resume Next
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = sList
.cc = cList
.bcc = bList
.subject = subjectStr
.Body = bodyStr
'.Send ->Just uncomment this line and remove the Display command to send the email without displaying
.Display ' this will make the email display
End With
On Error GoTo 0
x = x + 1
If UBound(dlMarkers) = x Then
Exit For
End If
Next
Set OutMail = Nothing
Set OutApp = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox "Error Number" & Err.Number & "-" & Err.Description
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function EmailList(addArr As Variant) As String
Dim t As Long
For t = LBound(addArr, 1) To UBound(addArr, 1)
If Len(addArr(t, 1)) > 0 Then
EmailList = EmailList & addArr(t, 1) & ";"
Else
Exit For
End If
Next
If Len(EmailList) > 0 Then
EmailList = Left(EmailList, Len(EmailList) - 1)
Else
EmailList = ""
End If
End Function
Function BodyList(addArr As Variant) As String
Dim t As Long, dtStr As String
BodyList = bodyString & vbCrLf & vbCrLf & dataHdrs & vbCrLf
For t = LBound(addArr, 1) To UBound(addArr, 1)
If Len(addArr(t, 1)) > 0 Then
dtStr = dtStr & addArr(t, 1) & Chr(9) & Chr(9) & Chr(9) & Chr(9) & addArr(t, 2) & Chr(9) & Chr(9) & addArr(t, 3) & _
Chr(9) & Chr(9) & "$" & " " & Format(addArr(t, 4), "#,##0.00") & vbCrLf
Else
Exit For
End If
Next
BodyList = BodyList & dtStr & vbCrLf & vbCrLf & signStr
'Debug.Print BodyList
End Function
Bookmarks