+ Reply to Thread
Results 1 to 6 of 6

Send Email Based on 2 Dates

Hybrid View

  1. #1
    Registered User
    Join Date
    05-12-2015
    Location
    Miami, FL
    MS-Off Ver
    365
    Posts
    49

    Send Email Based on 2 Dates

    Hello all, this is my first thread and have looked at others but can't find a solution.

    I need a VBA code that sends an email to the same email address every time TODAY() date matches the date on 2 difference cells.

    Please refer to the attached file:

    If TODAY() matches any date in Column D (i.e. D2 [10/1/2015]):

    Email should read: Hello John Doe, the contract for School Board Of Mdc (A2) will expire in four months from today, TODAY() on 2/1/2016 (B2).

    If TODAY() matches any date in Column C (i.e. C2 [8/1/2015]):

    Email should read: Hello John Doe, the contract for School Board Of Mdc (A2) will expire in six months from today, TODAY() on 2/1/2016 (B2).

    I am using Outlook 2013 and Excel 2013 on Windows 7 Professional.

    Thanks. I appreciate your help!
    Attached Files Attached Files
    Last edited by onelifestyle; 05-13-2015 at 03:36 PM.

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Send Email Based on 2 Dates

    How about:
    Sub EmailReminder()
    
    For i = 2 To 5
        For j = 3 To 4
        
        If Cells(i, j).Value = Date Then
    
        strBodyMonths = "six"
        If j = 4 Then strBodyMonths = "four"
    
        Dim OutApp As Object
        Dim OutMail As Object
        
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
            With OutMail
                .To = "[email protected]"
                .Subject = "Expiration Notice"
                .Body = "Hello John Doe, the contract for " & Cells(i, 1) & _
                        "will expire in " & strBodyMonths & " months from today, " & Date & " on " & Cells(i, 2) & "."
                .Send
            End With
    
        Set OutMail = Nothing
        Next
    Next
    
    End Sub
    Last edited by daffodil11; 05-13-2015 at 05:12 PM.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Registered User
    Join Date
    05-12-2015
    Location
    Miami, FL
    MS-Off Ver
    365
    Posts
    49

    Re: Send Email Based on 2 Dates

    Thank you daffodil11 for your reply. Unfortunately I get this error: Capture.JPG

    Please advise. Thanks again!

  4. #4
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Send Email Based on 2 Dates

    Oops. Missed an End If.

    Sub EmailReminder()
    
    For i = 2 To 5
        For j = 3 To 4
        
        If Cells(i, j).Value = Date Then
    
        strBodyMonths = "six"
        If j = 4 Then strBodyMonths = "four"
    
        Dim OutApp As Object
        Dim OutMail As Object
        
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
            With OutMail
                .To = "[email protected]"
                .Subject = "Expiration Notice"
                .Body = "Hello John Doe, the contract for " & Cells(i, 1) & _
                        "will expire in " & strBodyMonths & " months from today, " & Date & " on " & Cells(i, 2) & "."
                .Send
            End With
    
        End If
        Set OutMail = Nothing
        Next
    Next
    
    End Sub

  5. #5
    Registered User
    Join Date
    05-12-2015
    Location
    Miami, FL
    MS-Off Ver
    365
    Posts
    49

    Re: Send Email Based on 2 Dates

    When I transfer the code to the real spreadsheet I'm working with I get a Type Mismatch error. Please take a look at the code:

    Sub EmailReminder()
    
    FinalRow = Cells(Rows.Count, 2).End(xlUp).Row
    
    For i = 2 To FinalRow
        For j = 15 To 17
        
        If Cells(i, j).Value = Date Then
    
        strBodyMonths = "six"
        If j = 16 Then strBodyMonths = "four"
        If j = 17 Then strBodyMonths = "three"
    
        Dim OutApp As Object
        Dim OutMail As Object
        
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
            With OutMail
                .To = "[email protected]"
                .Subject = "Contract Expiration Notice for " & Cells(i, 2)
                .Body = "Hello Ron, the contract for " & Cells(i, 2) & _
                        " will expire in " & strBodyMonths & " months from today, " & Date & " on " & Cells(i, 14) & ". Have a wonderful day!"
                .Send
            End With
    
        End If
        Set OutMail = Nothing
        Next
    Next
    
    End Sub
    Thank you again!
    Last edited by onelifestyle; 05-22-2015 at 02:17 PM.

  6. #6
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Send Email Based on 2 Dates

    The code should let you know which line error'ed out.

    The most likely culprit is
    If Cells(i, j).Value = Date Then
    if the value being compared is an error itself.

    Try using

        If IsNumeric(Cells(i, j).Value) Then
            If Cells(i, j).Value = Date Then
    
            End If
        End If
    to try to supress the error.
    Last edited by daffodil11; 05-22-2015 at 04:18 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Workflow with signatures (if cell=x then send email, if signed then send email)
    By Kate2811 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2014, 05:37 AM
  2. Search multiple columns of dates and send reminder email 30 days before expiry
    By prh2013 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-20-2013, 12:47 AM
  3. Send delayed email from dates in cell's in excel
    By Ryan Nielsen in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-05-2013, 03:06 PM
  4. Conditional formating that will send email to 5 oldest dates in a column
    By ayan1988 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-23-2013, 11:25 AM
  5. Check dates and then send email to user.
    By dutchdog in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-10-2012, 03:36 AM

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