I have finally managed to amend the code and it it working 100%
Sub GenerateEmails()
Dim OutApp As Object
Dim OutMail As Object
Dim ws As Worksheet
Dim EmailAddresses As Range
Dim SubjectText As String
Dim BodyText As String
Dim AttachmentPath As String
Dim RecipientName As String
Dim AttachedWb As Workbook
Dim AverageScore As Variant
Application.DisplayAlerts = False
' Create or get the Outlook application
On Error Resume Next
Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0
' Loop through sheets from "BR1" to the last sheet
For Each ws In ThisWorkbook.Sheets
If ws.Index >= ThisWorkbook.Sheets("BR1").Index Then
' Check if AA1 cell is not blank, A1 cell is not blank, and AB1 is not equal to 0
If Not IsEmpty(ws.Range("AA1:AA2").Value) Then
' Check if Col E from Row 2 onwards contains only one item
If WorksheetFunction.CountA(ws.Range("E2:E" & ws.Cells(Rows.Count, "E").End(xlUp).Row)) = 1 Then
' Get the value of the single item
Dim SingleItemValue As Double
SingleItemValue = ws.Range("E2").Value
' Check if the single item value exceeds 60
If SingleItemValue > 60 Then
' Create a new Outlook mail item
Set OutMail = OutApp.CreateItem(0) ' 0 represents olMailItem
' Get the email addresses from the current cell in Range AA1 to AA2
Set EmailAddresses = ws.Range("AA1:AA2")
' Concatenate email addresses into a single string separated by semicolons
Dim ToEmails As String
ToEmails = Join(Application.Transpose(EmailAddresses.Value), ";")
RecipientName = Trim(ws.Range("Z1").Value)
' Set the email subject from the "Email" sheet
SubjectText = ThisWorkbook.Sheets("Email Branches").Range("B1").Value
' Set the email body text from the "Email" sheet
BodyText = ThisWorkbook.Sheets("Email Branches").Range("BodyText1").Value
' Find "Regards" and "Howard" in BodyText and insert two line breaks before them
BodyText = Replace(BodyText, "Regards", "<br><br>Regards")
BodyText = Replace(BodyText, "Howard", "<br><br>Howard")
' Create a new Outlook mail item
With OutMail
.To = ToEmails
.Subject = SubjectText
.HTMLBody = BodyText
AttachmentPath = ThisWorkbook.Path & "\Inventory Units-" & ws.Name & ".xlsx"
ThisWorkbook.Sheets(ws.Name).Copy
Set AttachedWb = ActiveWorkbook
AttachedWb.SaveAs AttachmentPath
.Attachments.Add AttachmentPath
AttachedWb.Close SaveChanges:=False ' Close the attached sheet without saving
.Display ' Display the email
End With
End If
Else
' Compute the average of Col E
On Error Resume Next
AverageScore = Application.WorksheetFunction.Average(ws.Range("E2:E" & ws.Cells(Rows.Count, "E").End(xlUp).Row))
On Error GoTo 0
' Check if there was an error in calculating the average
If IsError(AverageScore) Then
' Handle the error (you might want to skip or do something else)
Exit For
End If
' Check if average is less than 61, skip creating email
If AverageScore > 60 Then
' Create a new Outlook mail item
Set OutMail = OutApp.CreateItem(0) ' 0 represents olMailItem
' Get the email addresses from the current cell in Range AA1 to AA2
Set EmailAddresses = ws.Range("AA1:AA2")
' Concatenate email addresses into a single string separated by semicolons
' Dim ToEmails As String
ToEmails = Join(Application.Transpose(EmailAddresses.Value), ";")
RecipientName = Trim(ws.Range("Z1").Value)
' Set the email subject from the "Email" sheet
SubjectText = ThisWorkbook.Sheets("Email Branches").Range("B1").Value
' Set the email body text from the "Email" sheet
BodyText = ThisWorkbook.Sheets("Email Branches").Range("BodyText1").Value
' Find "Regards" and "Howard" in BodyText and insert two line breaks before them
BodyText = Replace(BodyText, "Regards", "<br><br>Regards")
BodyText = Replace(BodyText, "Howard", "<br><br>Howard")
' Create a new Outlook mail item
With OutMail
.To = ToEmails
.Subject = SubjectText
.HTMLBody = "Hi " & RecipientName & "<br><br>" & BodyText
AttachmentPath = ThisWorkbook.Path & "\Inventory Units-" & ws.Name & ".xlsx"
ThisWorkbook.Sheets(ws.Name).Copy
Set AttachedWb = ActiveWorkbook
AttachedWb.SaveAs AttachmentPath
.Attachments.Add AttachmentPath
AttachedWb.Close SaveChanges:=False ' Close the attached sheet without saving
.Display ' Display the email
End With
End If
End If
End If
End If
Next ws
' Clean up Outlook objects
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Bookmarks