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
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.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
COUNTIFS not COUNTIF...So close, yet so far away! Thank you very much Donkey!
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.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
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.
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)
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
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.
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?
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!
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
not quite sure re: ranges (altered for sake of demo) but you would want either=COUNT(IF(BM!D497:BM!D497>=A3-7,1,0)*IF(BM!D497>BM!D497<=A3-1,1,0))}
=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))
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
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...
YES! Victory!
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.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
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)}
=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)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks