so i had a chance to properly review your code
it seems you had two running variables "cap" and "mess" to change essentially one line of Body
this seems unnecessary and can lead to confusion so i combined into one
also got rid of mutliple CountA lines and replaced them with one variable that you calculate once
Sub victory()
Dim outlookApp As Object 'mailapp object
Dim OutlookMailItem As Object 'mailitem object
Dim Icounter As Integer 'counter for loop
Dim MailDest As String 'email destination
Dim Mess As String 'message
Dim Subject As String 'subject
Dim j As Long 'counter for times email appears
Dim lRow As Long 'last Row of table
Dim f As Range 'range for Find and FindNext loop
Dim RangeToSearch As Range 'define the range of what to search
Dim fFirst As Range 'fariable for first found range
'use this to determine last row once instead of doing it multiple times within the loop
lRow = WorksheetFunction.CountA(Columns(7))
For Icounter = 2 To lRow
'reset after each email
Set outlookApp = CreateObject("outlook.application")
Set OutlookMailItem = outlookApp.CreateItem(0)
MailDest = ""
Subject = ""
Mess = ""
j = 0
If Cells(Icounter, 9) = "Captured" Then
With OutlookMailItem
'determine how many times emails with "captured" shows up
j = Application.CountIfs(Range("G2:G" & lRow), Cells(Icounter, 7), Range("I2:I" & lRow), Cells(Icounter, 9))
If j = 1 Then
Mess = Cells(Icounter, 4).Value & " - " & Cells(Icounter, 8).Value ' combined Cap and Mess into one instead of having two variables to manage
MailDest = Cells(Icounter, 7).Value
Subject = Cells(Icounter, 2).Value & " " & Cells(Icounter, 4).Value & " Opportunities"
Cells(Icounter, 9) = "Sent to CSA"
Else
Set RangeToSearch = Range("G2:G" & lRow) 'define range to search
Set f = RangeToSearch.Find(Cells(Icounter, 7).Value, lookat:=xlPart, LookIn:=xlValues) 'set first find
Set fFirst = f 'set first address
'maildest and subject stay the same
MailDest = Cells(f.Row, 7).Value
Subject = "New Capability Opportunities"
'define first row of message and update "To send" column
Mess = Cells(f.Row, 4).Value & " - " & Cells(f.Row, 8).Value
Cells(f.Row, 9) = "Sent to CSA"
Do Until f Is Nothing
Set f = RangeToSearch.FindNext(f) 'set next find
If f.Address = fFirst.Address Then Exit Do 'exit loop criteria
If Cells(f.Row, 9) = "Captured" Then
Mess = Mess & vbNewLine & Cells(f.Row, 4).Value & " - " & Cells(f.Row, 8).Value 'update mess
Cells(f.Row, 9) = "Sent to CSA" 'update To Send
End If
Loop
End If
.To = MailDest 'not sure why you were using .cc instead of .to
.Subject = Subject
.Body = "List of Opportunities:" & _
vbNewLine & vbNewLine & Mess & vbNewLine & vbNewLine & _
"Refer to spreadsheet for complete list of all opportunities!"
'change to however you want email to be displayed, use vbnewline to enter new line
.Display '.Send change to .send for automatic send, use .display when debugging
End With
Set OutlookMailItem = Nothing
Set outlookApp = Nothing
End If
Next Icounter
End Sub
attached new file
hopefully this one works properly now
Bookmarks