Multiple conditions in excel 2002 - excel

I am using excel 2002 to create a spreadsheet. What I am trying to do is use the countif function but I have more than one condition. So I have 2 columns with a list of numbers and what I want to say is count the number of occurences where the number x is in one column and the number y is in the other column (in the same row).
E.g.
1 1
1 1
1 2
2 2
2 3
3 3
So if in the above I wanted to count the rows where the first column had the number 1 and the second column had the number 2, the answer should be 1.
I can't use the COUNTIF function because that only allows you to specify one condition and the COUNTIFS isn't available because I am using excel 2002.
Please Help.

This is a job for an array formula. In your case you can do:
=SUM((A1:A6=1)*(B1:B6=2))
entered as an array forumla (Ctrl-Shift-Enter)
The equality tests each return an array of boolean values, and the multiplication does an element-wise multiply (so a logical AND in this case). SUM coerces booleans to numbers when it adds up the resulting array.
EDIT:
In an answer to this similar question:
https://stackoverflow.com/questions/576569/use-2-conditions-in-excel-sumif
Jon Fournier posted a link to:
http://www.cpearson.com/excel/ArrayFormulas.aspx
which has a lot more detail on this sort of thing.

You could create a 3rd column which joins the two other column values with (say) a space or other special character, and then test for the combined value with COUNTIF.
If you don't want to add another column on your worksheet, you could put it on a different worksheet - or even create a dynamic named range.

Personally I love the SUMPRODUCT function but I'm not sure if you have it in Excel2002
Here's a very good resource for multiple conditions:
http://www.ozgrid.com/Excel/sum-if.htm

The DCOUNT function allows for multiple criteria.
The formula would look like this
=DCOUNT(A2:B8,"ColumnA",A11:B12)
if you have your criteria below the data.
Screenshot of data and formula http://img31.imageshack.us/img31/1093/dcount.png

Related

Sum row based on criteria across multiple columns

I have googled for hours, not being able to find a solution to what I need/want. I have an Excel sheet where I want to sum the values in one column based on the criteria that either one of two columns should have a specific value in it. For instance
A B C
1 4 20 7
2 5 100 3
3 100 21 4
4 15 21 4
5 21 24 8
I want to sum the values in C given that at least one of A and B contains a value of less than or equal to 20. Let us assume that A1:A5 is named A, B1:B5 is named B, and C1:C5 is named C (for simplicity). I have tried:
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
which gives me the rows where both columns match summed twice, and
={SUMPRODUCT(C,((A<=20)*(C<=20)))}
which gives me only the rows where both columns match
So far, I have settled for the solution of adding a column D with the lowest value of A and B, but it bugs me so much that I can't do it with formulas.
Any help would be highly appreciated, so thanks in advance. All I have found when googling is the "multiple criteria for same column" problem.
Thanks. That works. Found another one that works, after I figured out that excel does not treat 1 + 1 = 1 as I learnt in discrete mathematics, but as you say, counts the both the trues. Tried instead with:
{=SUM(IF((A<=20)+(B<=20);C;0))}
But I like yours better.
Your problem that it is "summing twice" in this formula
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
is due to addition turning first TRUE plus the second TRUE into 2. It is not actually summing twice, because for any row, if only one condition is met, it would count that row only once.
The solution is to transform either the 1 or the 2 into a 1, using an IF:
={SUMPRODUCT(C,IF((A<=20)+(C<=20))>0, 1, 0)}
That way, each value in column C would only be counted at max once.
Following this site you could build up your SUMPRODUCT() formula like this:
=SUMPRODUCT(C,SIGN((A<=20)+(C<=20)))
So, instead of a nested IF() you control your or condition with the SIGN()function.
hth
If you plan to use a large set of data then it is best to use the array formula:
{=SUM(IF((A1:A5<=20)+(B1:B5<=20),C1:C5,0))}
Obviously adjust the range to suit the data set, however if the whole of each column is to form part of the formula then you can simply adjust to:
{=SUM(IF((A:A<=20)+(B:B<=20),C:C,0))}
This will perform the calculation on all rows of data within the A, B and C columns. With either example remember to press Ctrl + Shift + Enter in order to trigger the array formula (as opposed to typing the { and }).

Get Max number in column based on 2 Lookups

I need a formula to:
Lookup in its own column for the largest #
only if conditions are met in column 1 and 2
Add 1 to the found number and insert it into the cell
In other words, I want to "filter" my list for rows that have X in column 1 and Y in column 2
and then get the largest number from column 3 within these rows.
Then add 1 to that number.
This can be achieved with a array formula:
{=MAX(IF(($A$1:$A$1000="x")*($B$1:$B$1000="y"),$C$1:$C$1000))+1}
Input:
=MAX(IF(($A$1:$A$1000="x")*($B$1:$B$1000="y"),$C$1:$C$1000))+1
and then [Ctrl]+[Shift]+[Enter] to create a array formula.
Then should there be automatically the curly brackets around the formula.
Greetings
Axel
Standard (non-array) equivalent for MAXIF() functionality would look like:
=MAX(INDEX(C2:C999*(A2:A999="X")*(B2:B999="Y"),,))+1
With your added condition for adding 100 to numbers less than 100 then,
=MAX(INDEX((C2:C999+(C2:C999<100)*100)*(A2:A999="X")*(B2:B999="Y"),,))+1
For versions of Excel that are 2010 and higher, the AGGREGATE¹ function can use the conditional criteria to throw errors then ignore the errors when processing the MAX function.
    
The standard formulas for E5:E6 are,
=AGGREGATE(14, 6, $C$2:$C$99/(($A$2:$A$99="X")*($B$2:$B$99="Y")), 1)
=AGGREGATE(14, 6, ($C$2:$C$99+($C$2:$C$99<100)*100)/(($A$2:$A$99="X")*($B$2:$B$99="Y")), 1)
In fact, AGGREGATE is using its LARGE sub-function with 1 as the k parameter. Locating the second, third, etc. largest values which meet the criteria is a simple matter of sequencing the k parameter. This can be done with ROW(1:1) then filling the formula down.
¹AGGREGATE¹ was introduced in Excel 2010. It is not available in previous versions.

SUMIF with OR criteria

How to use SUMIF formula in Excel cell that must sum over a given range and instead of finding for a single value, it should find multiple values?
For finding a single value, I use:
=SUMIF(A4:A100;"1";B4:B100)
Now I need to sum over if the column A holds 1 or 2, like:
=SUMIF(A4:A100;"1" OR "2";B4:B100)
The cell A1 will hold the criteria as a text, here it would be 1;2.
It should return same as
=SUMIF(A4:A100;"1";B4:B100) + SUMIF(A4:A100;"2";B4:B100)
but I need a formula that can take any number of criteria (1,2,3,... or more).
What's the syntax? I'm not able to use VBA here.
To sum for 1 or 2 try this version
=SUM(SUMIF(A4:A100;{1;2};B4:B100))
SUMIF will return an "array" of two results so you need SUM to sum that array for the total for 1 and 2
You can add as many numbers as you like e,g,
=SUM(SUMIF(A4:A100;{1;2;3;4};B4:B100))
or with numbers listed in a range like Z1:Z10
=SUMPRODUCT(SUMIF(A4:A100;Z1:Z10;B4:B100))
I don't think there is a way to do OR within a single statement like this. You can use SUMIFS for multiple conditions where all need to be true, but in this case you would just need to add together multiple SUMIF statements:
=SUMIF(A4:A100,"1",B4:B100)+SUMIF(A4:A100,"2",B4:B100)
Since "1" and "2" are mutually exclusive:
=SUMIF(A4:A100,"1",B4:B100)+SUMIF(A4:A100,"2",B4:B100)
i think you should define a range, let's say keys where you keep all values for which you want to sum. so in this range you keep 1 and 2 and can modyfy it whenever you want. then you add a flag column with formula IFERROR(IF(MATCH(A4,keys,0)>0,1,0),0) - now you have column in which 1 is for the values you want to sum.
this works with multiple text evaluation
=sumif(M4:M206,"Sat",O4:O206)+sumif(M4:M206,"Sun",O4:O206) // add here more + + +

How to COUNTIFS with an array of criteria and match NOT condition?

My data table is like the image above. I can easily count the number of Male participants in group B or C using this array formula:
=SUM(COUNTIFS($B:$B, $E3, $C:$C, $F3:$F4))
The result is 3 as expected. However I'm gonna do the reverse thing, that is count the number of Male participants in NOT group B or C. The result should be 1 but currently I'm stuck at this.
Can anybody show me a way please (preferably not just counting the number of all Male participants and then do a subtraction)? I have even tried to change the values in the Group to something like <>B and <>C but it just doesn't work.
As you only have 2 in the group you can easily use COUNTIFS with 2 separate criteria, i.e.
=COUNTIFS($B:$B,$E3,$C:$C,"<>"&$F3,$C:$C,"<>"&$F4)
but clearly that might not be desirable for a large group, so you could use SUMPRODUCT like this to reference the group once
=SUMPRODUCT(($B:$B=$E3)*ISNA(MATCH($C:$C,$F3:$F4,0)))
ISNA will exclude matching rows - to include use ISNUMBER
You can replace F3:F4 with any single row or column of values
Note: whole columns with SUMPRODUCT will work (post Excel 2003) but is undesirable as Jerry says
Hmm, the thing with the formula right now is that the first COUNTIFS (for F3) will return 2 and the second COUNTIFS (for F4) will return 3, which SUM converts into 5 when you try:
=SUM(COUNTIFS($B:$B, $E3, $C:$C, "<>"&$F3:$F4))
I would suggest using SUMPRODUCT instead:
=SUMPRODUCT(($B:$B=$E3)*($C:$C<>$F3)*($C:$C<>$F4))
And maybe make the range smaller since this can take some time (you don't need to insert this as an array formula).
Otherwise, another option would be to count all the Males, and then subtract the counts for group B and subsequently C:
=COUNTIF($B:$B, $E3)-SUM(COUNTIFS($B:$B, $E3, $C:$C, $F3:$F4))

In Excel 2007, how can I SUMIFS indices of multiple columns from a named range?

I am analysing library statistics relating to loans made by particular user categories. The loan data forms the named range LoansToApril2013. Excel 2007 is quite happy for me to use an index range as the sum range in a SUMIF:
=SUMIF(INDEX(LoansToApril2013,0,3),10,INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6))
Here 10 indicates a specific user category, and this sums loans made to that group from three columns. By "index range" I'm referring to the
INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6)
sum_range value.
However, if I switch to using a SUMIFS to add further criteria, Excel returns a #VALUE error if an index range is used. It will only accept a single index.
=SUMIFS(INDEX(LoansToApril2013,0,4),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
works fine
=SUMIFS(INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
returns #value, and I'm not sure why.
Interestingly,
=SUMIFS(INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,4),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
is also accepted and returns the same as the first one with a single index.
I haven't been able to find any documentation or comments relating to this. Does anyone know if there is an alternative structure that would allow SUMIFS to conditionally sum index values from three columns? I'd rather not use three separate formulae and add them together, though it's possible.
The sumifs formula is modelled after an array formula and comparisons in the sumifs need to be the same size, the last one mimics a single column in the LoansToApril2013 array column 4:4 is column 4.
The second to bottom one is 3 columns wide and the comparison columns are 1 column wide causing the error.
sumifs can't do that, but sumproduct can
Example:
X 1 1 1
Y 2 2 2
Z 3 3 3
starting in A1
the formula =SUMPRODUCT((A1:A3="X")*B1:D3) gives the answer 3, and altering the value X in the formula to Y or Z changes the returned value to the appropriate sum of the lines.
Note that this will not work if you have text in the area - it will return #VALUE!
If you can't avoid the text, then you need an array formula. Using the same example, the formula would be =SUM(IF(A1:A3="X",B1:D3)), and to enter it as an array formula, you need to use CTRL+SHIFT+ENTER to enter the formula - you should notice that excel puts { } around the formula. It treats any text as zero, so it will successfully add up the numbers it finds even if you have text in one of the boxes (e.g. change one of the 1's in the example to be blah and the total will be 2 - the formula will add the two remaining 1s in the line)
The two answers above and a bit of searching allowed me to find a formula that worked. I'll put it here for posterity, because questions with no final outcome are a pain for future readers.
=SUMPRODUCT( (INDEX(LoansToApril2013,0,3)=C4) * (INDEX(LoansToApril2013,0,1)="PTFBL") * INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6))
This totals up values in columns 4-6 of the LoansToApril2013 range, where the value in column 3 equals the value in C4 (a.k.a. "the cell to the left of this one with the formula") AND the value in column 1 is "PTFBL".
Despite appearances, it isn't multiplying anything by anything else. I found an explanation on this page, but basically the asterisks are adding criteria to the function. Note that criteria are enclosed in their own brackets, while the range isn't.
If you want to use names ranges you need to use INDIRECT for the Index commands.
I used that formula to check for conditions in two columns, and then SUM the results in a table which has 12 columns for the months (the column is chosen by a helper cell which is 1 to 12 [L4]).
So you can do if:
Dept (1 column name range [C6]) = Sales [D6];
Region (1 column name range [C3]) = USA [D3];
SUM figures in the 12 column monthly named range table [E7] for that 1 single month [L4] for those people/products/line item
Just copy the formula across your report page which has columns 1-12 for the months and you get a monthly summary report with 2 conditions.
=SUMPRODUCT( (INDEX(INDIRECT($C$6),0,1)=$D$6) * (INDEX(INDIRECT($C$3),0,1)=$D$3) * INDEX(INDIRECT($E7),0,L$4))

Resources