+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    COUNTIF function Using Dates

    Hello all,

    First post so forgive me in advance if posting is incorrect. Straight to the point. Here is what I am trying to do:

    I have a column of contract dates (6/7/06, 8/14/09 etc.) and I would like to set up a cell to count the number of contracts written between specific dates. So, for instance, if I enter the date 10/12/09 in cell A1, I would want cell A2 to count the number of contracts written between 10/5/09 and 10/11/09.

    This was my thought: COUNTIF(b1:b300,AND(b1:b300>=(a1-7),b1:b300<=a1-1)

    This formula does not return an error, but does not give the correct output.

    Questions: Is COUNTIF the right function to use for this purpose? Can I use the AND function of I want to have multiple conditions? Also, is there always issues when adding and subtracting from dates (ie, a1-7) and is it best to avoid doing that if possible and just stick to referencing cells?

    Thanks in advance for any help.

    Brandon

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: COUNTIF function Using Dates

    If you're using XL2007 as your profile implies you can make use of the COUNTIFS function.

    =COUNTIFS(B1:B300,">="&A1-7,B1:B300,"<="&A1-1)

    if not (re: 2007) you're looking at SUMPRODUCT but the above is more efficient.

  3. #3
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    COUNTIFS not COUNTIF...So close, yet so far away! Thank you very much Donkey!

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: COUNTIF function Using Dates

    Yes, COUNTIFS was introduced in XL2007 along with the likes of SUMIFS, AVERAGEIF, AVERAGEIFS - none are thus backwards compatible with earlier versions of XL - in earlier versions you would be looking at either SUMPRODUCT or Arrays to conduct multiple criteria aggregations.

  5. #5
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    That sounds very handy. Unfortunately I realized as I returned from holiday that I am using Excel 2003 not 2007. I am working on getting an upgrage so I do not have to venture down the Array or Sumproduct road but will refer to your "reccomended reading" if need be.

    Thanks again.

  6. #6
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: COUNTIF function Using Dates

    OK but for ref.

    =COUNTIFS(B1:B300,">="&A1-7,B1:B300,"<="&A1-1)

    could be replaced by

    =SUMPRODUCT(--(ROUNDUP((B1:B300-A1)/7,0)=-1))

    If you want to do more complex date calcs (ie other than 1 week prior) you can use a similar construct to the COUNTIFS

    =SUMPRODUCT(--(B1:B300>=A1-7),--(B1:B300<=A1-1))

    the latter is the more intuitive approach (in addition to being more flexible)

  7. #7
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    Would you mind translating those formulas into words for me? I would love to understand what they mean so I can use/tweak them as needed in the future.

  8. #8
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    I was using your tutorial on sumproduct when I saw your reply and this was as far as I had gotten:

    {=COUNT(IF(BM!D497:BM!D497>=A3-7,1,0)*IF(BM!D497>BM!D497<=A3-1,1,0))}

    Should that work as well?

  9. #9
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: COUNTIF function Using Dates

    In all honesty the SUMPRODUCT link in my sig. is about as good as you will/can get... I wouldn't/don't want to detract from it with my own ramblings on the subject!

  10. #10
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: COUNTIF function Using Dates

    =COUNT(IF(BM!D497:BM!D497>=A3-7,1,0)*IF(BM!D497>BM!D497<=A3-1,1,0))}
    not quite sure re: ranges (altered for sake of demo) but you would want either

    =COUNT(IF((BM!D497:D1000>=A3-7)*(BM!D497:D1000<=A3-1),1))
    CSE

    or just

    =SUM((BM!D497:D1000>=A3-7)*(BM!D497:D1000<=A3-1))
    CSE

    (and not my tutorial unfortunately, author is Bob Phillips (MS Excel MVP))

  11. #11
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    So in conclusion, with a single array being used, the COUNT function with two IFs should get the job done. And when I need to use two arrays SUMPRODUCT is the only way for me to go...

  12. #12
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    YES! Victory!

  13. #13
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,379

    Re: COUNTIF function Using Dates

    Quote Originally Posted by wbmcwhorter View Post
    So in conclusion, with a single array being used, the COUNT function with two IFs should get the job done. And when I need to use two arrays SUMPRODUCT is the only way for me to go...
    Not entirely sure I grasp the above but I would put it like this and again I stress this is aimed primarily at those running a version pre XL2007...

    If you have a single condition you should adopt standard COUNTIF/SUMIF functions as these are incredibly efficient.

    If you have multiple conditions, then first see if you create a singular condition based on helper cells utilising concatenation... if you can do so and revert to the standard COUNTIF & SUMIF functions as outlined already, this approach will be more efficient.

    If you can not create a single condition (ie concatenation etc not viable) then you're traditionally left with a choice between:

    SUMPRODUCT

    and

    CSE Array
    (CSE being CTRL + SHIFT + ENTER of course)

    It's generally accepted that there is little difference between the two in terms of efficiency... SUMPRODUCT is processed in the same way as an Array - on that basis neither are quick.

    The main advantages of SUMPRODUCT over CSE is the fact it doesn't require CSE (!) so from an end users perspective it is perhaps a little more robust.

    CSE Arrays have an advantage over SUMPRODUCT in so far as they are more flexible - eg handling underlying error values in source data etc etc...

    If you find yourself conducting multiple SUMPRODUCT formulae - say to create an Average then I would generally say you're better off reverting to a single array, eg instead of

    =SUMPRODUCT(--(A1:A100="x"),--(B1:B100="y"),C1:C100)/SUMPRODUCT(--(A1:A100),--(B1:B100="y"))

    I would opt for

    =AVERAGE(IF((A1:A100="x")*(B1:B100="y"),C1:C100))
    CSE

    I hope that clears things a little.

    (there are also the Database functions like DGET etc but they are seen less often though they are certainly no less efficient, many would argue the opposite but they can be trickier to setup)
    Last edited by DonkeyOte; 11-30-2009 at 11:45 AM.

  14. #14
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    Makes sense. After going through your post and reading the SUMPRODUCT link a few times, I am getting a much better grasp of how to handle single and multiple arrays.

    Can you tell me if you see anything wrong with this SUMPRODUCT formula (since I do not have COUNTIFS capability) ?

    {=SUMPRODUCT(MONTH(Inventory!F6:F287)=MONTH(A3))*(YEAR(Inventory!F6:F287)=YEAR(A3))*(Inventory!F6:F2 87)}

  15. #15
    Registered User
    Join Date
    11-26-2009
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2002
    Posts
    9

    Re: COUNTIF function Using Dates

    =SUMPRODUCT(--(MONTH(Inventory!F6:F287)=MONTH(A3)),--(YEAR(Inventory!F6:F287)=YEAR(A3)),(Inventory!J6:J287))

    I am guessing this looks format better to you? (ignoring cell ref of course)

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.2.0