+ Reply to Thread
Results 1 to 6 of 6

If-Then-Else Question

  1. #1
    Registered User
    Join Date
    04-17-2012
    Location
    Lancaster, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    If-Then-Else Question

    I have a routine that sends emails when certain criteria are met. This works fine, but I would like it to not send anything if the Days >10. Any thoughts? Thanks for you assistance.
    Please Login or Register  to view this content.
    Last edited by Paul; 04-18-2012 at 02:45 PM. Reason: Fixed CODE tags to wrap the code.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: If-Then-Else Question

    Hi JFreeland, without seeing the rest of your procedure, it's hard to say. As it is, perhaps just exiting the procedure might suffice.
    Please Login or Register  to view this content.
    Replace Sub with Function if you are working in a function.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    04-17-2012
    Location
    Lancaster, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: If-Then-Else Question

    Yes, that works fine. Tried that before, but I think I used End instead of exit. Thanks for your quick response. I appreciate your help.

  4. #4
    Registered User
    Join Date
    04-17-2012
    Location
    Lancaster, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: If-Then-Else Question

    Mordred, I'm finding that anything with days over 10 stops the routine, which would make sense. Since I have a list of items with various dates, I need it to loop through all of the rows before stopping and choose those that meet the criteria.

  5. #5
    Registered User
    Join Date
    04-17-2012
    Location
    Lancaster, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: If-Then-Else Question

    This is my sub in it's entirety:

    Sub SendEmail()
    'Uses late binding
    Dim OutlookApp As Object
    Dim MItem As Object
    Dim cell As Range
    Dim Subj As String
    Dim EmailAddr As String
    Dim Recipient As String
    Dim Msg As String
    Dim Document As String
    Dim Days As Variant

    'Create Outlook object
    Set OutlookApp = CreateObject("Outlook.Application")


    'Loop through the rows
    For Each cell In Columns("D").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "*@*" Then
    'Get the data
    Subj = "Compliance Reminder"
    Recipient = cell.Offset(0, -3).Value
    EmailAddr = cell.Value
    Document = cell.Offset(0, -1).Value
    Days = cell.Offset(0, 2).Value

    'Compose message

    If Days > 10 Then
    Exit Sub
    ElseIf Days < 10 Then
    Msg = Recipient & vbCrLf & vbCrLf
    Msg = Msg & "This is a live test of the Regulatory Agency Report compliance system. "
    Msg = Msg & "This message was automatically generated in Excel. Please disregard "
    Msg = Msg & "the message below, it is "
    Msg = Msg & "for test purposes only. Please let me know that you received this email." & vbCrLf & vbCrLf & vbCrLf
    Msg = Msg & "This is a reminder that the "
    Msg = Msg & Document
    Msg = Msg & " is due to be filed in "
    Msg = Msg & Days
    Msg = Msg & " days. Please let me know if you have filed this report"
    Msg = Msg & " or when you expect to file it." & vbCrLf & vbCrLf
    Msg = Msg & "Regards," & vbCrLf & vbCrLf
    Msg = Msg & "John Freeland" & vbCrLf
    Msg = Msg & "Staff Accountant" & vbCrLf
    Msg = Msg & "Rocky Brands Inc LLC" & vbCrLf
    Msg = Msg & "Phone:740-753-9100 x2804" & vbCrLf
    Msg = Msg & "john.freeland@rockybrands.com"
    ElseIf Days < 0 Then
    Msg = Recipient & vbCrLf & vbCrLf
    Msg = Msg & "This is a reminder that the "
    Msg = Msg & Document
    Msg = Msg & " is now overdue and requires immediate attention."
    End If

    'Create Mail Item and send it
    Set MItem = OutlookApp.CreateItem(0) 'olMailItem
    With MItem
    .To = EmailAddr
    .Subject = Subj
    .Body = Msg
    '.Send
    .Save 'to Drafts folder
    End With
    End If
    Next
    Set OutlookApp = Nothing
    End Sub

  6. #6
    Registered User
    Join Date
    04-17-2012
    Location
    Lancaster, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: If-Then-Else Question

    Got it fixed!

+ 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