+ Reply to Thread
Results 1 to 5 of 5

Thread: How to trigger excel to send an email on a date instead of a number value?

  1. #1
    Registered User
    Join Date
    12-06-2010
    Location
    San Antonio, Texas
    MS-Off Ver
    Excel 2007
    Posts
    2

    How to trigger excel to send an email on a date instead of a number value?

    I'm trying to set up an excel database to automatically send out reminder emails when due dates for my employees' recertifactions are 2 weeks away. Actually sending an email seems to work prefectly with the code I am using so I didn't list that portion, but I do not know how to change the trigger for sending an email from numeric (currently is target cell>200) to instead equaling a date (specifically, trying to get it to send when cells equal current date+14). I even think with the code below that the trigger could just be changed to when target cell>current date+14 so not as much would have to be changed.

    Thanks in advance.


    
    Option Explicit
    
    Private Sub Worksheet_Calculate()
        Dim FormulaRange As Range
        Dim NotSentMsg As String
        Dim MyMsg As String
        Dim SentMsg As String
        Dim MyLimit As Double
    
        NotSentMsg = "Not Sent"
        SentMsg = "Sent"
    
        'Above the MyLimit value it will run the macro
        MyLimit = 200
    
        'Set the range with the Formula that you want to check
        Set FormulaRange = Me.Range("B8")
    
        On Error GoTo EndMacro:
        For Each FormulaCell In FormulaRange.Cells
            With FormulaCell
                If IsNumeric(.Value) = False Then
                    MyMsg = "Not numeric"
                Else
                    If .Value > MyLimit Then
                        MyMsg = SentMsg
                        If .Offset(0, 1).Value = NotSentMsg Then
                            Call Mail_with_outlook1
                        End If
                    Else
                        MyMsg = NotSentMsg
                    End If
                End If
                Application.EnableEvents = False
                .Offset(0, 1).Value = MyMsg
                Application.EnableEvents = True
            End With
        Next FormulaCell
    
    ExitMacro:
        Exit Sub
    
    EndMacro:
        Application.EnableEvents = True
    
        MsgBox "Some Error occurred." _
             & vbLf & Err.Number _
             & vbLf & Err.Description
    
    End Sub
    Last edited by Leith Ross; 12-06-2010 at 11:39 AM. Reason: Added Code Tags

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: How to trigger excel to send an email on a date instead of a number value?

    Not tested but maybe:

    Private Sub Worksheet_Calculate()
    Dim FormulaRange As Range
    Dim NotSentMsg As String
    Dim MyMsg As String
    Dim SentMsg As String
    Dim MyLimit As Date
    
    NotSentMsg = "Not Sent"
    SentMsg = "Sent"
    
    'Above the MyLimit value it will run the macro
    MyLimit = Date + 14
    
    'Set the range with the Formula that you want to check
    Set FormulaRange = Me.Range("B8")
    
    On Error GoTo EndMacro:
    For Each FormulaCell In FormulaRange.Cells
    With FormulaCell
    If IsNumeric(.Value) = False Then
    MyMsg = "Not numeric"
    Else
    If .Value = MyLimit Then
    MyMsg = SentMsg
    If .Offset(0, 1).Value = NotSentMsg Then
    Call Mail_with_outlook1
    End If
    Else
    MyMsg = NotSentMsg
    End If
    End If
    Application.EnableEvents = False
    .Offset(0, 1).Value = MyMsg
    Application.EnableEvents = True
    End With
    Next FormulaCell
    
    ExitMacro:
    Exit Sub

    Please use code tags when you post code to the forum.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: How to trigger excel to send an email on a date instead of a number value?

    Hello b_hill06,

    Welcome to the Forum!

    To make your posts easier to read, copy, and edit please wrap your code. I did it for you this time. Here is how you can do it next time.

    How to wrap your Code using the # icon
    1. Select all your code using the mouse.
    2. Click on the # icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post.

    To manually wrap your code, use these Bulletin Board Code Tags
    [code] Before of the first line,
    [/code] After the last line.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    12-06-2010
    Location
    San Antonio, Texas
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: How to trigger excel to send an email on a date instead of a number value?

    Leith, I appreciate the pointers. Next time I'll post correctly.

    Domski, thank you but I'm still not getting an email to come up when I input 12/21/2010 (today's date + 15 to trigger an email) into cell B8 for the due date. I'm thinking instead of target cell>Date + 14 it needs to be target cell<Date + 14 so I would need a lower limit instead of an upper limit. I feel like the porblem may also have something to do with the part of the code that says "Not numeric" because that's what shows up in cell C8 when I enter a date instead of a number. Any other ideas?

  5. #5
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: How to trigger excel to send an email on a date instead of a number value?

    Yeah, IsDate should be used in VBA to test for a date. You said "specifically, trying to get it to send when cells equal current date+14)" which is why I used = but if you want it to pick up before or after then you would need to use either < or >.

    Along these lines I guess but adjust to suit:

    Private Sub Worksheet_Calculate()
    
        Dim FormulaRange As Range
        Dim FormulaCell As Range
        Dim NotSentMsg As String
        Dim MyMsg As String
        Dim SentMsg As String
        Dim MyLimit As Date
    
        NotSentMsg = "Not Sent"
        SentMsg = "Sent"
    
    'Above the MyLimit value it will run the macro
        MyLimit = Date + 14
    
    'Set the range with the Formula that you want to check
        Set FormulaRange = Me.Range("B8")
    
        On Error GoTo EndMacro:
    
        For Each FormulaCell In FormulaRange.Cells
            With FormulaCell
                If IsDate(.Value) = False Then
                    MyMsg = "Not date"
                Else
                    If .Value = MyLimit Then
                        MyMsg = SentMsg
                        If .Offset(0, 1).Value = NotSentMsg Then
                            Call Mail_with_outlook1
                        End If
                    Else
                        MyMsg = NotSentMsg
                    End If
                End If
                Application.EnableEvents = False
                .Offset(0, 1).Value = MyMsg
                Application.EnableEvents = True
            End With
        Next FormulaCell
        
    EndMacro:
        
    End Sub

    Dom
    Last edited by Domski; 12-06-2010 at 02:24 PM.
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0