• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Code to Email when a task is 30 days or less away - Not Working

Sphinxy1542

New Member
I am using the code below to populate an email when a task is 30 days or less away. This should populate upon opening excel.
It initially worked, but was pulling up all dates regardless of when they were.

I have added in the below, but it's now not working at all. Would be very grateful for some assistance.

Dim rowDate As Long, today As Long

And

today = Date
rowDate = xRgDate.Offset(I - 1).Value
If rowDate - today <= 30 And rowDate - today > 0 Then

Full Code below, but workbook also attached.

Private Sub Workbook_Open()
Dim xRgDate As Range
Dim xRgSend As Range
Dim xRgText As Range
Dim xRgDone As Range
Dim xOutApp As Object
Dim xMailItem As Object
Dim xLastRow As Long
Dim vbCrLf As String
Dim xMailBody As String
Dim xRgDateVal As String
Dim xRgSendVal As String
Dim xMailSubject As String
Dim rowDate As Long, today As Long
Dim I As Long
On Error Resume Next
Set xRgDate = Range("A1:A2", Range("A" & Rows.Count).End(xlUp)) ' Email Address
xLastRow = xRgDate.Rows.Count
Set xRgDate = xRgDate(1)
Set xRgSend = Cells(xRgDate.Row, "B") ' Reminder Content
Set xRgText = Cells(xRgDate.Row, "C") ' Completion Deadline Date
Set xOutApp = CreateObject("Outlook.Application")
For I = 1 To xLastRow
xRgDateVal = xRgDate.Offset(I - 1).Value
today = Date
rowDate = xRgDate.Offset(I - 1).Value
If rowDate - today <= 30 And rowDate - today > 0 Then
xRgSendVal = xRgSend.Offset(I - 1).Value
xMailSubject = xRgText.Offset(I - 1).Value & " on " & xRgDateVal
vbCrLf = "<br><br>"
xMailBody = "<HTML><BODY>"
xMailBody = xMailBody & "Dear " & xRgSendVal & vbCrLf ' Greeting
xMailBody = xMailBody & "Text : " & xRgText.Offset(I - 1).Value & vbCrLf
xMailBody = xMailBody & "</BODY></HTML>"
Set xMailItem = xOutApp.CreateItem(0)
With xMailItem
.Subject = xMailSubject
.To = xRgSendVal
.HTMLBody = xMailBody
.Display
'.Send
End With
Set xMailItem = Nothing
End If
Next
Set xOutApp = Nothing
End Sub
 

Attachments

  • Chandoo Upload.xlsm
    21.2 KB · Views: 13
Sphinxy1542
I can just guess
... because, I cannot test this ...

I modified 'some parts' of Your code.
You could test this ...
 

Attachments

  • Chandoo Upload.xlsm
    18.5 KB · Views: 20
Sphinxy1542
I can just guess
... because, I cannot test this ...

I modified 'some parts' of Your code.
You could test this ...

This is great, but shows up all reminders even if the date has passed.
Please can you advise on how I can get this to only open an email if a task is 30 days away or less away? Many thanks for your help.
 
@Sphinxy1542,

In your original code, try changing these lines:

Code:
        today = DateAdd("d", 30, Date)
        rowDate = xRgDate.Offset(I - 1, 2).Value
        If rowDate < today Then

Thanks/Ajesh
 
Back
Top