+ Reply to Thread
Results 1 to 6 of 6

Automated Email in Excel

  1. #1
    Registered User
    Join Date
    05-23-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Red face Automated Email in Excel

    Hi All,

    I have a spread that generates an automatic email when a contract is due to end.

    Currently, if the contract is due to end in the next 20 days an email will be generated.

    I would really like to set this up so that I can generate a second email detailing that it is URGENT if the contract is due to end in 10 day.

    I have attached my spread to this sheet.

    If you can help I would be so appreciative.

    Alice :-)
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Automated Email in Excel

    How about something like this, where after ensuring the 3rd column is not nothing (""), we use a Select Case construct to handle the scenarios (modifications in red)? Please test and report back results.

    Please Login or Register  to view this content.
    Edit: <5 minutes after posting> I just made three changes to the orignal syntax I provided. Please ensure you have copied the last version. If the Select Case is evaluating lngDateSpread, you're good.
    Last edited by AlvaroSiza; 05-23-2013 at 09:57 AM.
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Registered User
    Join Date
    05-23-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Automated Email in Excel

    Sub CheckForExpiryDates()

    Dim Cell As Range
    Dim ExpiryDate As Date
    Dim Mail_Msg As String
    Dim Mail_Subj As String
    Dim Mail_USubj As String
    Dim Rng As Range
    Dim RngEnd As Range
    Dim lngDateSpread As Long

    Mail_Subj = "Training"
    Mail_USubj = "URGENT: Training"


    Set Rng = Worksheets("Sheet1").Range("A2")
    Set RngEnd = Rng.Parent.Cells(Rows.Count, Rng.Column).End(xlUp)
    Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Rng.Parent.Range(Rng, RngEnd))

    For Each Cell In Rng.Cells
    ExpiryDate = Cell.Offset(0, 2)
    lngDateSpread = DateDiff("d", Now(), ExpiryDate)
    If Cell.Offset(0, 3) = "" Then
    Select Case DateDiff
    Case Is <= 10
    Mail_Msg = Cell.Value & "'s Contract is due to expire on " & ExpiryDate & vbCrLf _
    & "please advise on extension"
    SendEmail Cell.Offset(0, 1), Mail_Subj, Mail_Msg
    Cell.Offset(0, 3) = Now()
    Case 11 To 20
    Mail_Msg = Cell.Value & "'s Contract is due to expire on " & ExpiryDate & vbCrLf _
    & "please advise on extension"
    SendEmail Cell.Offset(0, 1), Mail_USubj, Mail_Msg
    Cell.Offset(0, 3) = Now()
    Case Else 'do nothing
    End Select
    End If
    Next Cell

    End Sub

    It says that the arguement is not optional and highlights the datediff I have enlarged above.

    Thanks!

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Automated Email in Excel

    You got to it before my "Edit" comment at the end of my post. My apologies. Change DateDiff to lngDateSpread.

  5. #5
    Registered User
    Join Date
    05-23-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Automated Email in Excel

    This seems to be working perfectly now!! Thanks so much. I am going to fiddle more and will report back any errors.

    Many Thanks!

  6. #6
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Automated Email in Excel

    You bet. Hope it works out for you.

+ 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.6.0 RC 1