I am currently using the following code:
Sub SendEmail()
Dim objOutlook As Object
Dim objMail As Object
Dim RowsCount As Integer
Dim Index As Integer
Dim Recipients As String
Dim Category As String
Dim CellReference As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
If ActiveSheet.FilterMode = True Then
RowsCount = ActiveSheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
ElseIf ActiveSheet.FilterMode = False Then
RowsCount = Application.CountA(Range("A2:A" & Rows.Count)) - 1
End If
' In Range("I1") there is the job category the user wants to email
Category = Range("I1")
If Category = Range("S2") Then
' CellReference is the amount of columns to the right of column A, ie Column A is 0 so CellReference below is J - which is the column location of the email address according to that category
CellReference = 10
ElseIf Category = Range("S3") Then
CellReference = 14
ElseIf Category = Range("S4") Then
CellReference = 18
ElseIf Category = Range("S5") Then
CellReference = 16
End If
Index = 0
While Index < RowsCount
Set EmailAdrs = ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, CellReference).Offset(0 + Index, 0)
Recipients = Recipients & EmailAdrs.Value & ";"
Index = Index + 1
Wend
With objMail
.To = Recipients
.Subject = "This is the subject"
.Display
End With
Set objOutlook = Nothing
Set objMail = Nothing
End Sub
This code checks to see if a filter has been applied and counts the number of lines, if it is one or more, then it checks who should be sent by email (the “Category” located at I1this place of work of different people), and then receives the addresses of the email that is required, the problem I am facing says that I have the following data (this is just an example of what I want to do):
Column A Column B Column C
Smith Male 123@123.co.uk
Jones Male abc@abc.co.uk
Smith Female 456@123.co.uk
Jones Female def@abc.co.uk
Smith Male 789@123.co.uk
Smith Female 101112@123.co.uk
Smith Female 141516@123.co.uk
Jones Female ghi@abc.co.uk
Jones A Female B, , def@abc.co.uk ghi@abc.co.uk def@abc.co.uk 789@123.co.uk, , , .
, ?
, , A, B, A B.