snuffnchess
Board Regular
- Joined
- May 15, 2015
- Messages
- 71
- Office Version
- 365
- Platform
- Windows
I have a list of email addresses that exist is columns C through H that belong to a company listed in A and am needing to essentially combine those emails into one variable so that when an email is created, it sends to to everybody.
Because there are different numbers of columns filled with data, and we cannot have extra ";" if there are blanks, how does this get coded?
Here is what the email data looks like in current format, and the format it is needing to be in
Current code to email data
Because there are different numbers of columns filled with data, and we cannot have extra ";" if there are blanks, how does this get coded?
Here is what the email data looks like in current format, and the format it is needing to be in
email.xlsx | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | |||
1 | Given | Needed | ||||||||||||
2 | Name | Team | Email 1 | Email 2 | Email 3 | Email 4 | Email 5 | Email 6 | Name | Team | ||||
3 | Company 1 | Team 3 | c11firstlast@email.com | c12firstlast@email.com | c13firstlast@email.com | Company 1 | Team 3 | c11firstlast@email.com; c12firstlast@email.com; c13firstlast@email.com | ||||||
4 | Company 2 | Team 1 | c21firstlast@email.com | c22firstlast@email.com | Company 2 | Team 1 | c21firstlast@email.com; c22firstlast@email.com | |||||||
5 | Company 3 | Team T | c31firstlast@email.com | c32firstlast@email.com | c33firstlast@email.com | c34firstlast@email.com | c35firstlast@email.com | Company 3 | Team T | c31firstlast@email.com; c32firstlast@email.com; c33firstlast@email.com; c34firstlast@email.com; c35firstlast@email.com | ||||
6 | Company 4 | Team T | c41firstlast@email.com | c42firstlast@email.com | c43firstlast@email.com | c44firstlast@email.com | c45firstlast@email.com | Company 4 | Team T | c41firstlast@email.com; c42firstlast@email.com; c43firstlast@email.com; c44firstlast@email.com; c45firstlast@email.com | ||||
7 | Company 5 | Team 1 | c51firstlast@email.com | c52firstlast@email.com | c53firstlast@email.com | c54firstlast@email.com | c55firstlast@email.com | Company 5 | Team 1 | c51firstlast@email.com; c52firstlast@email.com; c53firstlast@email.com; c54firstlast@email.com; c55firstlast@email.com | ||||
8 | Company 6 | Team T | c61firstlast@email.com | c62firstlast@email.com | Company 6 | Team T | c61firstlast@email.com; c62firstlast@email.com | |||||||
9 | Company 7 | Team 2 | c71firstlast@email.com | Company 7 | Team 2 | c71firstlast@email.com | ||||||||
10 | Company 8 | Team 3 | c81firstlast@email.com | Company 8 | Team 3 | c81firstlast@email.com | ||||||||
11 | Company 9 | Team 2 | c91firstlast@email.com | Company 9 | Team 2 | c91firstlast@email.com | ||||||||
12 | ||||||||||||||
13 | ||||||||||||||
Sheet1 |
Current code to email data
Code:
Option Explicit
Public sfolder As String
Public sfile As String
Sub SendMail()
Dim objOutlook As Object
Dim objMail As Object
Dim emTo As String
Dim emRep As String
Dim emSubject As String
Dim emBody As String
Dim emAttach As String
Dim emdata As Worksheet
Dim ob As Workbook
Set ob = ThisWorkbook
Set emdata = ob.Sheets("Email")
emBody = "<p><b>Hello</b></p>" & "<p>Here is your file</p>" & "<p> Do not ask us any questions</p>"
sfolder = emdata.Range("I1").Value
Set objOutlook = CreateObject("Outlook.Application")
Dim r As Long: For r = 2 To ActiveSheet.Range("A2").End(xlDown).Row
With ActiveSheet
emTo = .Range("C" & r).Value
emRep = .Range("B" & r).Value
emSubject = .Range("A" & r).Value & " - Report - " & Format(Now, "yyyy-mm-dd")
sfile = emdata.Range("A" & r).Value & ".xlsx"
emAttach = sfolder & "\" & sfile
End With
Set objMail = objOutlook.CreateItem(0)
With objMail
.To = emTo
.ReplyRecipients.Add emRep
.Subject = emSubject
.HTMLBody = "<html><head></head><body>" & em & "</body></html>"
.Attachments.Add emAttach
.Display
.Send
End With
Next
End Sub