+ Reply to Thread
Results 1 to 8 of 8

MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

  1. #1
    Registered User
    Join Date
    09-16-2009
    Location
    Toront
    MS-Off Ver
    Excel 2003
    Posts
    9

    MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    Hi All,

    I am trying to write some code that runs through a column in excel and checks if the date within each row in that column falls between two dates that are enter by the user as start date and end date. Unfortunately, it is not working!!

    Could someone please help me. It seems straightforward but somehow it doesnt quite work. Here is the code:

    Please Login or Register  to view this content.

    ***********
    If DStartdate = 08/30/2012 and DEndDate = 08/31/2012 then it seems to only count all records where date aka ActiveSheet.Range("H" & i) = 08/30/2012 but it ignores all dates that = 08/31/2012.

    I'm down to my last few hairs!! Please help.

    THANKS!!!
    Last edited by Cutter; 09-05-2012 at 07:11 AM. Reason: Added code tags

  2. #2
    Registered User
    Join Date
    02-29-2012
    Location
    Gloucester, England
    MS-Off Ver
    Excel, Word, Access 2007 and XP
    Posts
    58

    Re: MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    can I ask what you're doing with the result totNumFound?

    If you're displaying it on the sheet a formula may suffice.
    Dave

    I'm no expert I just like solving problems

  3. #3
    Registered User
    Join Date
    09-16-2009
    Location
    Toront
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    Totnumfound is being displayed on the sheet but is used in multiple loops through multiple arrays to generate multiple reports on a number of sheets. I'm adding this If statement to evaluate on date so that I can run the report for a particular date period instead of a 'snapshot' showing all records.

    The If statement does not fail but does not evaluate properly. The >= and <= should resolve to "between dateA and dateB" but for some reason excludes the records <= dateB.
    I just need someone to help me figure out why this is happening or alternatively correct the statement to work properly.

    PLEASE HELP!

  4. #4
    Registered User
    Join Date
    09-16-2009
    Location
    Toront
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    Hey Guys. I thought this would be a simple code fix. I have seen some impressive code on this forum and didnt think it would take long for someone to correct my code. Is there truly no one who can help with this.

  5. #5
    Registered User
    Join Date
    03-08-2012
    Location
    Notts
    MS-Off Ver
    Excel 2010
    Posts
    71

    Re: MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    I am not 100% on best way to solve this, but a possibility of making this easier could be to change the date formatting. If you read up on this page on ozgrid you will see what I mean:

    http://www.ozgrid.com/VBA/find-dates.htm

  6. #6
    Registered User
    Join Date
    09-16-2009
    Location
    Toront
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    Quote Originally Posted by mtt23 View Post
    I am not 100% on best way to solve this, but a possibility of making this easier could be to change the date formatting. If you read up on this page on ozgrid you will see what I mean:

    http://www.ozgrid.com/VBA/find-dates.htm
    Thanks. I will see if this is a factor.

  7. #7
    Registered User
    Join Date
    09-16-2009
    Location
    Toront
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    I managed to get it to work. I declared a starttime and endtime variable and assigned them the static values of 12:00:00 AM and 11:59:59 respectively. So I now had four separate variables for start date/time and end date/time all declared as date. I then tried to combine them into a single variable for start SDATETIME and end ENDDATETIME which I used in my IF Statement. This kept failing however though I tried many variation and also using CDATE. Finally it struck me that I should declare all the variables as string and then use CDATE in the IF Statement to convert the combined date/time string to a date. That finally worked.

    code is below in case anyone is interested.


    Please Login or Register  to view this content.

  8. #8
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: MACRO - IF Statement to evaluate if Date falls BETWEEN to Dates - Please help.

    @ tdguy09

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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