+ Reply to Thread
Results 1 to 10 of 10

If Statements with Dates

Hybrid View

jmarshall If Statements with Dates 11-18-2008, 12:21 AM
JBeaucaire Column A has the dates to be... 11-18-2008, 12:46 AM
jmarshall I couldn't get that to work,... 11-18-2008, 01:09 AM
jmarshall Does anybody know how to do... 11-24-2008, 12:03 AM
JBeaucaire Let's assume those dates are... 11-24-2008, 04:21 AM
Cheeky Charlie This is something of a... 11-24-2008, 05:31 AM
Cheeky Charlie JB, I like sumproduct... 11-24-2008, 05:38 AM
jmarshall Thanks guys. I could... 11-24-2008, 07:19 AM
Cheeky Charlie Are your dates actual dates... 11-24-2008, 07:39 AM
JBeaucaire Thank, Charlie, I originally... 11-24-2008, 02:08 PM
  1. #1
    Registered User
    Join Date
    11-18-2008
    Location
    Australia
    Posts
    4

    If Statements with Dates

    Simple, simple question. I have a list of dates and I need to calculate how many of the dates are before or between certain dates. I can't quite get my parameters correct.

    Thank you.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Column A has the dates to be searched
    B1 has the early date
    C1 has the late date
    
    =SUMPRODUCT((A1:A100>=B1)*(A1:A100<=C1))
      or
    =SUMPRODUCT((A1:A100>B1)*(A1:A100<C1))    <-- doesn't include the limiting dates, the first one does
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    11-18-2008
    Location
    Australia
    Posts
    4
    I couldn't get that to work, I probably wasn't clear enough. Let's try this to start, I have a list of dates as follows:

    15-Jul-08
    31-Aug-08

    28-Aug-08

    21-Aug-07

    And I'm just trying to get a count of the dates that are before August 2008 (hence 2).

    And would it be OK to have empty rows within the date data?

    Thanks.

  4. #4
    Registered User
    Join Date
    11-18-2008
    Location
    Australia
    Posts
    4
    Does anybody know how to do this? Thanks.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Let's assume those dates are in Column A. A simple straightforward formula would be:
    =SUMPRODUCT(--(A1:A100<DATE(2008,8,1)))
    This version assumes there may be blanks like in your original example above, this code deducts those:
    =SUMPRODUCT(--(A1:A100<DATE(2008,8,1)))--(-COUNTBLANK(A1:A100))
    You could opt to put the DATE in a cell so you can easily change the date you're setting as your cutoff, changing the code to:
    =SUMPRODUCT(--(A1:A100<C1))--(-COUNTBLANK(A1:A100))

  6. #6
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561
    This is something of a sledgehammer to crack a nut...

    Dates are stored by Excel as numbers, for example, today is 39766. To see this, type today's date in a cell, then format the cell to "general"...

    When you insert a date, Excel converts it to a number, so it can add a week, take away a year, etc. Via formatting, it then converts it back to something you can understand (39766 means very little to me).

    So to find dates earlier than today, you just need <39766!

    The best way, in my opinion, of inserting this information (i.e. that number) is with =date(year,month,date) which you can see JB using in the first formula in his second post. An alternative is datevalue("datestring") e.g. datevalue("18/06/2008"). I wuoldn't recommend this because it is unreliable across different conventions (i.e. UK date style = dd/mm/yy, US date style = mm/dd/yy) which can be confusing. date() has no ambiguity.

    In answer to this:
    And I'm just trying to get a count of the dates that are before August 2008 (hence 2).
    =COUNTIF(A1:A10,"<"&DATE(2008,8,1))

    HTH

  7. #7
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561
    JB,

    I like sumproduct formulae too, but they're actually quite slow and not always necessary; how about these revisions?:

    =SUMPRODUCT((A1:A100>=B1)*(A1:A100<=C1))
    =COUNTIF(A:A,"<="&C1)-COUNTIF(A:A,"<"&B1)

    This is faster, you can see this means you also don't have to revise the range sizes all the time as countif can operate on the entire column.

    =SUMPRODUCT(--(A1:A100<DATE(2008,8,1)))
    and
    =SUMPRODUCT(--(A1:A100<DATE(2008,8,1)))--(-COUNTBLANK(A1:A100))
    Neither simple nor straightforward! (countif example given in previous post operates faster, is easier to understand, and accounts for blanks!

    CC

  8. #8
    Registered User
    Join Date
    11-18-2008
    Location
    Australia
    Posts
    4
    Thanks guys.

    I could actually get the sumproduct calculations to work but not the COUNTIF calcs. I'm more familiar with the COUNTIF as well but I keep getting an Err508 messages. Could if have something to do with the way excel reads dates as mentioned above? Any idea how to solve that issue?

    Thanks.

  9. #9
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561
    Are your dates actual dates or are they dates stored as strings?

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492
    Thank, Charlie, I originally had your COUNTIF solution, too, except I couldn't make it work, and I knew it should. I had the syntax of the "<"&C1 laid out wrong. Thanks for the clarification.

+ 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