Hey all,
I have found the code below somewhere else and tried to adapt it with no success...
Beside the error I get here
h = h & "<th bgcolor=""e0e0e0"">" & rng.Cells(1, c) & "</th>"
I believe the issues are several...
I have attached a sample sheet if someone can help I would greatly appreciate it!
Basically I would have this macro called every Monday with a code that I already have as part of the workbook_open sub.
I want the code below to look in column E for every date within the next 14 days of today's date (6th to the 20th January 2023)
For example in the sample sheet it would be those employees with due date in column E from 12/01/2023 to the 20/01/2023.
Once found copy those rows (A:G) and send via email.
Also the code below should place a "Sent" in column F so that the email doesn't get sent again.
Eventually I will have two other separate codes doing the same for the 60 days (column H) and 90 days (column K) expiration dates...
P.S the tables changes as rows are added and deleted and there are a few formulas in it.
Thanks in advance!
Option Explicit
Sub Send_Table_autofilter_2()
UnprotectAllSheets
Dim wb As Workbook, ws As Worksheet, wsBody As Worksheet
Dim rng As Range, dtDue As Date, iDays As Long
Dim iLastRow As Long, iMailRow As Long, i As Long
Dim sDates As String, dtTimestamp As Date, sStatus As String
Dim lines As New collection
' delete existing MailBody Sheet
Set wb = ThisWorkbook
For Each ws In wb.Sheets
If ws.Name = "MailBody" Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
Next
' create new MailBody Sheet
Set wsBody = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.count))
wsBody.Name = "MailBody"
' header row
Set ws = wb.Worksheets("Probation")
With wsBody.Range("A1:H1")
.Value2 = ws.Range("A2:E2").Value2
.Font.Bold = True
End With
iMailRow = 1
' scan sheet for due in <= 14 days
' copy to MailBody
iLastRow = ws.Cells(Rows.count, "E").End(xlUp).Row
For i = 3 To iLastRow ' assume row2 is header
If IsDate(ws.Cells(i, "E")) Then
dtDue = ws.Cells(i, "E")
iDays = DateDiff("d", Date, dtDue)
sStatus = ws.Cells(i, "F")
'dtTimestamp = ws.Cells(i, "G")
ws.Cells(i, "X") = iDays
If iDays > 0 And iDays < 14 And sStatus <> "Sent" Then
iMailRow = iMailRow + 1
ws.Range("A" & i & ":E" & i).Copy wsBody.Range("A" & iMailRow)
lines.Add i, CStr(i)
End If
End If
Next
' check if any records in collection
If lines.count > 0 Then
' convert to html
sDates = Format(Date, "dd mmm yyyy") & " and " & Format(Date + 14, "dd mmm yyyy")
Call SendEmail(wsBody.UsedRange, sDates)
' record email sent
For i = 1 To lines.count
ws.Range("F" & lines(i)) = "Sent"
'ws.Range("G" & lines(i)) = Now()
Next
Else
MsgBox "No records due", vbInformation
End If
' delete temp
Application.DisplayAlerts = False
wsBody.Delete
Application.DisplayAlerts = True
End Sub
Sub SendEmail(MailBody As Range, sDates As String)
Const CSS = "<style>p{font:13px Verdana};</style>"
Dim msg As String, outApp, outMail
msg = "<p>Hello!" & "<br><br>" & _
"The following are due between " & sDates & _
"<br><br>Please take the appropriate action<br><br>Thank you!<br>"
'Create mail
Set outApp = CreateObject("Outlook.Application")
Set outMail = outApp.CreateItem(0)
With outMail
.To = "[email protected]"
.cc = "sSendCC"
.subject = "Due in next 14 days"
.HTMLBody = CSS & msg & RangetoHTML(MailBody)
.Display
'send
End With
'outApp.Quit
'Set outApp = Nothing
End Sub
Function RangetoHTML(rng As Range) As String
Dim h As String, c As Integer, r As Long
h = "<table cellspacing=""0"" cellpadding=""5"" border=""1"" style=""font:13px Verdana"">"
For r = 1 To rng.Rows.count
h = h & "<tr>"
For c = 1 To rng.Columns.count
If r = 1 Then ' header
h = h & "<th bgcolor=""e0e0e0"">" & rng.Cells(1, c) & "</th>"
Else
h = h & "<td>" & rng.Cells(r, c) & "</td>"
End If
Next
h = h & "</tr>"
Next
RangetoHTML = h & "</table>"
End Function
Bookmarks