Results 1 to 4 of 4

Send email containing rows 14 days before expiration due date

Threaded View

  1. #1
    Forum Contributor
    Join Date
    01-20-2022
    Location
    London
    MS-Off Ver
    2010
    Posts
    101

    Send email containing rows 14 days before expiration due date

    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
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding Expiration Date and Days Left For Expiration Problem
    By Newmord in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2018, 09:37 AM
  2. Send email reminder when there are 31 days before a date.
    By U.Ali in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-17-2017, 08:44 PM
  3. Replies: 4
    Last Post: 12-24-2015, 12:02 PM
  4. Use Hyperlink to send email 6 months before expiration date
    By mcivli65 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-28-2015, 01:24 PM
  5. VBA to send email 30 days before due date
    By tekken in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-28-2013, 09:22 AM
  6. Replies: 1
    Last Post: 09-09-2013, 03:46 PM
  7. auto search for expiration date and send email to users
    By mingchu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-08-2013, 06:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1