+ Reply to Thread
Results 1 to 9 of 9

Excel 2007 : Using COUNTIF to count dates stored as text

  1. #1
    Registered User
    Join Date
    12-11-2009
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    12

    Question Using COUNTIF to count dates stored as text

    I have several Excel 2007 spreadsheets containing dates stored as text in yyyy-mm-dd format. I'd like a formula that will count the number of dates in a row. For example, these are the contents of columns A-G in rows 1 & 2:

    ID#.......Date1...............Date2...............Date3................Date4................Date5...............Date6
    474......2000-06-18.....2000-09-10.....2002-07-28......2005-12-04.....2006-05-14......0001-01-01

    I want to count the number of dates in this row that are 2005 or later, so I tried to use this formula in cell H2:

    =COUNTIF(B2:G2,">=2005-01-01")

    I expected this to evaluate to 2, but instead it came out as 0. I wondered if maybe my syntax was wrong, so I tried this:

    =COUNTIF(B2:G2,">="&"2005-01-01")

    but that also came out to 0. When I tried this:

    =COUNTIF(B2:G2,">=""2005-01-01")

    it came out to 6.

    I've always been able to compare dates stored as text, so I'm not sure why COUNTIF isn't working properly. What am I missing? The sheer volume of dates involved makes converting them from text format to date format highly impractical.

    Thanks!
    Last edited by keris; 01-16-2011 at 07:29 PM.

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Using COUNTIF to count dates stored as text

    Here are two approaches that seem to work:

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Does that help?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    12-11-2009
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Using COUNTIF to count dates stored as text

    Those do help for that particular criterion - thanks! But what if I want the criterion to be a certain date? For example, how would I count the number of dates that are later than 2005-06-17? (I can just see myself needing to do this in the future.)

  4. #4
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Using COUNTIF to count dates stored as text

    Try then:

    =SUMPRODUCT(--(B2:G2+0>="2005-01-01"+0))
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  5. #5
    Registered User
    Join Date
    12-11-2009
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Using COUNTIF to count dates stored as text

    NBVC, that gives me a #VALUE! error ("A value used in the formula is of the wrong data type").

  6. #6
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Using COUNTIF to count dates stored as text

    It assumes that all the text strings in B2:G2 are valid dates formatted as yyyy-mm-dd

    do you have other text in that range?

  7. #7
    Registered User
    Join Date
    12-11-2009
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Using COUNTIF to count dates stored as text

    Nope...the dates are the only contents of that range. However, since you mentioned "valid" dates, I experimented just now by clearing the cells that contained 0001-01-01. With that done, your formula works. Could you explain how you got it? I'm confused by the +0 and because I'm not sure of the order of operations for the test. Would it work for selecting a date in the middle of the year, such as 2005-06-17?

  8. #8
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Using COUNTIF to count dates stored as text

    The +0 coerces text strings to numbers (if they can't be converted to numbers, you get the #VALUE error).

    The order of operations within the parentheses is... coerce each range with the +0, then check if the components of the left range is greater than or equal to the right components.

    Then the -- is another coercer (could have used +0 at the other end). This coerces the TRUE/FALSE results from the >= checks to 1/0 respectively, then Sumproduct sums the results.

  9. #9
    Registered User
    Join Date
    12-11-2009
    Location
    Michigan
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Using COUNTIF to count dates stored as text

    OK, that makes sense. Thanks for explaining it.

    Thanks, everybody!

+ 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