+ Reply to Thread
Results 1 to 2 of 2

when criteria does not match do nothing excel vba macro

  1. #1
    Registered User
    Join Date
    03-25-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    3

    when criteria does not match do nothing excel vba macro

    I am implementing a macro that checks column E for dates that are 7 days away from current date.
    If cell date - current date = 7
    then an email containing the row that has the matching cell, is sent to an email address to notify.
    This is my coding it works succesfully, except for one issue.


    Sub Workbook_Open()

    Dim rngStart As Range
    Dim rngEnd As Range
    Dim rngCell As Range
    Dim strHtmlHead As String
    Dim strHtmlFoot As String
    Dim strMsgBody As String
    Dim strMsg As String
    Dim objEmail As Object
    Dim OutlookApp As Object
    Dim OutlookMail As Object

    'On Error GoTo ErrHnd

    'only run if between midnight and 2AM
    'If Hour(Now) < 2 Then

    'setup basic HTML message header and footer


    'setup start of body of message
    strMsgBody = "The following task(s) are due in less than 7 days :"

    'Worksheet name
    With Worksheets("Sheet1")
    'set start of date range
    Set rngStart = .Range("E1")
    'find end of date range
    Set rngEnd = .Range("E" & CStr(Application.Rows.Count)).End(xlUp)

    'loop through all used cells in column G
    For Each rngCell In .Range(rngStart, rngEnd)
    'test if date is equal to 7 days from today
    If IsDate(rngCell.Value) Then
    If rngCell.Value - Int(Now) = 7 Then
    'add to message - use task name from column A (offset -3)
    'change as required
    strMsgBody = strMsgBody & "
    " & "
    " & "Task: " & rngCell.Offset(0, -3).Text _
    & " is due on " & rngCell.Text & "
    " & "
    " & "Therefore please take necessary action"
    End If
    End If
    Next rngCell

    'Note last test time/date
    rngEnd.Offset(1, -3) = Now
    rngEnd.Offset(1, -3).NumberFormat = "dd/mm/yy"
    End With

    'put message together
    strMsg = strMsgBody

    'test message
    'MsgBox strMsg

    'create the e-mail object


    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)

    With OutlookMail

    .To = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = "Task Alert"
    .HTMLBody = strMsg
    .Send
    End With


    Set OutlookMail = Nothing
    Set OutlookApp = Nothing

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True


    'remove the e-mail object

    Exit Sub

    'error handler
    ErrHnd:
    Err.Clear

    End Sub


    It works succesfully, except for one issue.

    When no dates fulfill the criteria

    rngCell.Value - Int(Now) = 7

    An email is still generated without specifying any tasks.

    i want to edit the code so that no email is sent when there are no dates fullfilling the following criteria

    rngCell.Value - Int(Now) = 7

    How can i achieve this?

  2. #2
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: when criteria does not match do nothing excel vba macro

    Sending this email is done unconditionaly.
    When looking for task raise some flag if a task is found.
    Then when sending the email test if the flag raised first otherwise skip the email part.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

+ 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. [SOLVED] VBA Macro for extract data to new sheet with match criteria.
    By santosh226001 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 09-16-2014, 10:43 AM
  2. Macro to Match Column Against Row Criteria
    By Robert105 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-12-2014, 01:17 PM
  3. Macro, to match criteria in one column and copies results into a new tab
    By masond3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-04-2013, 08:57 AM
  4. Macro to pull all rows of data that match criteria into its own table in a new sheet
    By balston2 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2013, 02:08 PM
  5. Replies: 2
    Last Post: 03-31-2009, 11:04 AM

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