Sub create_multiple_emails()
Dim wb As Workbook, sh As Worksheet, c As Range, m As Range
Dim sBody As String, wFile As String
Dim dam As Object, dict As Object
'
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'
Set sh = ActiveSheet
Set dict = CreateObject("scripting.dictionary")
If sh.AutoFilterMode Then sh.AutoFilterMode = False
sh.Range("A1").AutoFilter Field:=20, Criteria1:=xlFilterToday, Operator:=xlFilterDynamic
For Each c In sh.Range("C2", sh.Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
If Not dict.exists(c.Value) Then
dict(c.Value) = dict(c.Value)
sh.Range("A1").AutoFilter 3, c
Set wb = Workbooks.Add
sh.AutoFilter.Range.EntireRow.Copy Range("A1")
wFile = ThisWorkbook.Path & "\" & Format(Date, "dd-mm-yyyy") & " " & c.Value & ".xlsx"
wb.SaveAs wFile
wb.Close False
Set dam = CreateObject("Outlook.Application").CreateItem(0)
'
'Mail Information
dam.To = "[COLOR=#0000ff]supplier1@supplier1.com; supplier2@supplier2.com; etc; etc[/COLOR]"
dam.Subject = "Subject ?????"
dam.Body = "Hi XXX, Please see attached. Regards XXX"
dam.Attachments.Add wFile
dam.Display 'use .Send to send
End If
Next
sh.ShowAllData
MsgBox "Emails sent"
End Sub
That works but I need the name in the supplier column to relate to the suppliers email address, I don't want every supplier to receive every email
Sub create_multiple_emails()
Dim wb As Workbook, sh As Worksheet, c As Range, m As Range
Dim sBody As String, wFile As String
Dim dam As Object, dict As Object
Dim sh2 As Worksheet, f As Range, EmailAddress As String, j As Long
'
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'
Set sh = ActiveSheet
Set sh2 = Sheets("Supplier Contact Details")
Set dict = CreateObject("scripting.dictionary")
If sh.AutoFilterMode Then sh.AutoFilterMode = False
sh.Range("A1").AutoFilter Field:=20, Criteria1:=xlFilterToday, Operator:=xlFilterDynamic
For Each c In sh.Range("C2", sh.Range("C" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
If Not dict.exists(c.Value) Then
dict(c.Value) = dict(c.Value)
sh.Range("A1").AutoFilter 3, c
Set f = sh2.Range("B:B").Find(c, , xlValues, xlWhole)
If Not f Is Nothing Then
For j = 3 To sh2.Cells(f.Row, Columns.Count).End(xlToLeft).Column
EmailAddress = EmailAddress & ";" & sh2.Cells(f.Row, j)
Next
Else
EmailAddress = [COLOR=#ff0000]"email@yahoo.com"[/COLOR] 'Mail in case there is no mail on sheet2
End If
Set wb = Workbooks.Add
sh.AutoFilter.Range.EntireRow.Copy Range("A1")
wFile = ThisWorkbook.Path & "\" & Format(Date, "dd-mm-yyyy") & " " & c.Value & ".xlsx"
wb.SaveAs wFile
wb.Close False
Set dam = CreateObject("Outlook.Application").CreateItem(0)
'
'Mail Information
dam.To = EmailAddress
dam.Subject = "Subject ?????"
dam.Body = "Hi XXX, Please see attached. Regards XXX"
dam.Attachments.Add wFile
dam.Display 'use .Send to send
End If
Next
sh.ShowAllData
MsgBox "Emails sent"
End Sub