You're welcome.
Shariq: to send one email remove the email preparation from the loop and instead record the engine numbers to a string (strEngines).
Dion
Sub Send_Email_Using_VBA()
Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String, strEngines As String
Dim ws As Object
Set ws = ThisWorkbook.Sheets("Sheet1")
'Loop through rows from 1 to last row
For varRow = 1 To ws.Range("A60000").End(xlUp).Row
'Check if hours exceed 100
If ws.Cells(varRow, 2).Value > 100 Then
strEngines = strEngines & vbNewLine & ws.Cells(varRow, 1).Value
End If
Next varRow
If strEngines = "" Then
MsgBox "There are no engines due for maintenance.", vbInformation
Exit Sub
End If
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strto = "fred@foss.com"
strcc = "shariq1989@gmail.com"
strbcc = ""
strsub = "Maintenance Alert"
strbody = "Attn." & vbNewLine & vbNewLine & _
"The following engines are due for maintenance:" & _
vbNewLine & vbNewLine & strEngines & _
vbNewLine & vbNewLine & "Automated Msg from the America"
With OutMail
.To = strto
.CC = strcc
.Subject = strsub
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
Set ws = Nothing
End Sub
Bookmarks