Hello! I currently have a Macro that creates an email for each row of data, then copies and pastes that row into the email. Each row is assigned to an individual (receiver of the email),, but sometimes, the same individual is assigned to multiple rows. I wanted to see if I could change up the code so that, if the same person is assigned multiple rows, that the whole group of rows will be copied to the email. This would minimize the number of emails I'm sending out to each person every morning.
Here's a snippet of the code I have right now. There is a bunch more code involved, this is just the code relevant to my question.
'Create the variables for the loop
Dim LastRw As Long, FirstRw As Long
Dim Rw As Long
'Create variables needed for RangeToHTML
Dim rng As Range
Dim rngHeader As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Selection.SpecialCells(xlCellTypeVisible)
With ActiveSheet
'Define First and Last Rows
FirstRw = 2
LastRw = sht.UsedRange.Rows(.UsedRange.Rows.Count).Row
'Create loop to go through all rows
For Rw = LastRw To FirstRw Step -1
'If the due date is equal to today, run code:
If sht.Range("G" & Rw).Value = Date Then
'Set Variables for RangeToHTML Function:
Set rng1 = sht.Range("A" & Rw).SpecialCells(xlCellTypeVisible)
Set rng2 = sht.Range("B" & Rw).SpecialCells(xlCellTypeVisible)
Set rng3 = sht.Range("C" & Rw).SpecialCells(xlCellTypeVisible)
Set rng4 = sht.Range("D" & Rw).SpecialCells(xlCellTypeVisible)
Set rng5 = sht.Range("E" & Rw).SpecialCells(xlCellTypeVisible)
Set rng6 = sht.Range("F" & Rw).SpecialCells(xlCellTypeVisible)
Set rng7 = sht.Range("G" & Rw).SpecialCells(xlCellTypeVisible)
Set rng = Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7)
Set rngHeader = sht.Range("A1:G1").SpecialCells(xlCellTypeVisible)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Create Signature
With OutMail
.Display
End With
'Create Email
With OutMail
.Subject = "Reminder - " & sht.Range("A" & Rw).Value & " Due Today"
'.Subject = "Reminder - CAR(s) Due Today"
.To = sht.Range("B" & Rw).Value
.CC = "Brianna Willson"
.HTMLBody = "Good Morning," & "<br>" & "<br>" & "This is a friendly reminder that you have a Corrective Action" & "<b>" _
& " due today." & "</b>" & " Please let me or Brianna Willson know if you require any additional support. " & "<br>" & "<br>" _
& "<u>" & "Please note, an extension cannot be requested as we are past the extension request due date. This action must be" & _
" completed today to avoid escalation." & "</u>" & "<br>" & "<br>" & RangetoHTML(rngHeader) & RangetoHTML(rng) & "<br>" & "<br>" _
& "Thank You," & "<br>" & "<br>" & .HTMLBody
.Display
End With
Set OutApp = Nothing
Set OutMail = Nothing
ElseIf sht.Range("G" & Rw).Value = Date + 2 Or ((Format(Now(), "DDD") = "Thu" Or Format(Now(), "DDD") = "Fri") And .Range("G" & Rw).Value = Date + 4) Then
'Set Variables for RangeToHTML Function:
Set rng1 = sht.Range("A" & Rw).SpecialCells(xlCellTypeVisible)
Set rng2 = sht.Range("B" & Rw).SpecialCells(xlCellTypeVisible)
Set rng3 = sht.Range("C" & Rw).SpecialCells(xlCellTypeVisible)
Set rng4 = sht.Range("D" & Rw).SpecialCells(xlCellTypeVisible)
Set rng5 = sht.Range("E" & Rw).SpecialCells(xlCellTypeVisible)
Set rng6 = sht.Range("F" & Rw).SpecialCells(xlCellTypeVisible)
Set rng7 = sht.Range("G" & Rw).SpecialCells(xlCellTypeVisible)
Set rng = Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7)
Set rngHeader = sht.Range("A1:G1").SpecialCells(xlCellTypeVisible)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Create Signature
With OutMail
.Display
End With
'Create Email
With OutMail
.Subject = "Reminder - " & sht.Range("A" & Rw).Value & " Upcoming Due Date"
'.Subject = "Reminder - CAR(s) Upcoming Due Dates"
.To = sht.Range("B" & Rw).Value
.CC = "Brianna Willson"
.HTMLBody = "Good Morning," & "<br>" & "<br>" & "This is a friendly reminder that you have a Corrective Action due in " & "<b>" _
& "two business days." & "</b>" & " Please let me or Brianna Willson know if you require any additional support or would like to" _
& " request an extension." & "<br>" & "<br>" & "<b>" & "<u>" & "<span style='background:yellow;mso-highlight:yellow'>" & _
"Please request an extension today or tomorrow; extensions cannot be requested the day the action is due." & _
"</span>" & "</b>" & "</u>" & "<br>" & "<br>" & "<br>" & RangetoHTML(rngHeader) & RangetoHTML(rng) & "<br>" & "<br>" & "Thank You," & _
"<br>" & "<br>" & .HTMLBody
.Display
End With
Set OutApp = Nothing
Set OutMail = Nothing
End If
Next Rw
End With
Here's a snippet of the code I have right now. There is a bunch more code involved, this is just the code relevant to my question.
'Create the variables for the loop
Dim LastRw As Long, FirstRw As Long
Dim Rw As Long
'Create variables needed for RangeToHTML
Dim rng As Range
Dim rngHeader As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Selection.SpecialCells(xlCellTypeVisible)
With ActiveSheet
'Define First and Last Rows
FirstRw = 2
LastRw = sht.UsedRange.Rows(.UsedRange.Rows.Count).Row
'Create loop to go through all rows
For Rw = LastRw To FirstRw Step -1
'If the due date is equal to today, run code:
If sht.Range("G" & Rw).Value = Date Then
'Set Variables for RangeToHTML Function:
Set rng1 = sht.Range("A" & Rw).SpecialCells(xlCellTypeVisible)
Set rng2 = sht.Range("B" & Rw).SpecialCells(xlCellTypeVisible)
Set rng3 = sht.Range("C" & Rw).SpecialCells(xlCellTypeVisible)
Set rng4 = sht.Range("D" & Rw).SpecialCells(xlCellTypeVisible)
Set rng5 = sht.Range("E" & Rw).SpecialCells(xlCellTypeVisible)
Set rng6 = sht.Range("F" & Rw).SpecialCells(xlCellTypeVisible)
Set rng7 = sht.Range("G" & Rw).SpecialCells(xlCellTypeVisible)
Set rng = Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7)
Set rngHeader = sht.Range("A1:G1").SpecialCells(xlCellTypeVisible)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Create Signature
With OutMail
.Display
End With
'Create Email
With OutMail
.Subject = "Reminder - " & sht.Range("A" & Rw).Value & " Due Today"
'.Subject = "Reminder - CAR(s) Due Today"
.To = sht.Range("B" & Rw).Value
.CC = "Brianna Willson"
.HTMLBody = "Good Morning," & "<br>" & "<br>" & "This is a friendly reminder that you have a Corrective Action" & "<b>" _
& " due today." & "</b>" & " Please let me or Brianna Willson know if you require any additional support. " & "<br>" & "<br>" _
& "<u>" & "Please note, an extension cannot be requested as we are past the extension request due date. This action must be" & _
" completed today to avoid escalation." & "</u>" & "<br>" & "<br>" & RangetoHTML(rngHeader) & RangetoHTML(rng) & "<br>" & "<br>" _
& "Thank You," & "<br>" & "<br>" & .HTMLBody
.Display
End With
Set OutApp = Nothing
Set OutMail = Nothing
ElseIf sht.Range("G" & Rw).Value = Date + 2 Or ((Format(Now(), "DDD") = "Thu" Or Format(Now(), "DDD") = "Fri") And .Range("G" & Rw).Value = Date + 4) Then
'Set Variables for RangeToHTML Function:
Set rng1 = sht.Range("A" & Rw).SpecialCells(xlCellTypeVisible)
Set rng2 = sht.Range("B" & Rw).SpecialCells(xlCellTypeVisible)
Set rng3 = sht.Range("C" & Rw).SpecialCells(xlCellTypeVisible)
Set rng4 = sht.Range("D" & Rw).SpecialCells(xlCellTypeVisible)
Set rng5 = sht.Range("E" & Rw).SpecialCells(xlCellTypeVisible)
Set rng6 = sht.Range("F" & Rw).SpecialCells(xlCellTypeVisible)
Set rng7 = sht.Range("G" & Rw).SpecialCells(xlCellTypeVisible)
Set rng = Union(rng1, rng2, rng3, rng4, rng5, rng6, rng7)
Set rngHeader = sht.Range("A1:G1").SpecialCells(xlCellTypeVisible)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Create Signature
With OutMail
.Display
End With
'Create Email
With OutMail
.Subject = "Reminder - " & sht.Range("A" & Rw).Value & " Upcoming Due Date"
'.Subject = "Reminder - CAR(s) Upcoming Due Dates"
.To = sht.Range("B" & Rw).Value
.CC = "Brianna Willson"
.HTMLBody = "Good Morning," & "<br>" & "<br>" & "This is a friendly reminder that you have a Corrective Action due in " & "<b>" _
& "two business days." & "</b>" & " Please let me or Brianna Willson know if you require any additional support or would like to" _
& " request an extension." & "<br>" & "<br>" & "<b>" & "<u>" & "<span style='background:yellow;mso-highlight:yellow'>" & _
"Please request an extension today or tomorrow; extensions cannot be requested the day the action is due." & _
"</span>" & "</b>" & "</u>" & "<br>" & "<br>" & "<br>" & RangetoHTML(rngHeader) & RangetoHTML(rng) & "<br>" & "<br>" & "Thank You," & _
"<br>" & "<br>" & .HTMLBody
.Display
End With
Set OutApp = Nothing
Set OutMail = Nothing
End If
Next Rw
End With