This question already has answers here:
Countif With Multiple OR Criteria
(4 answers)
Closed 8 years ago.
I have been googling this for a while and can´t seem to get it to work. I use Excel 2010 and want to count rows on a mix of AND and OR operators.
What I want to do is something like this
COUNTIFS($A:$A,"string1" , $B:$B,"string2" , $C:$C,{"stringA","stringB","stringC"})
This means mixing both AND and OR operator in the COUNTIFS function. Col A and col B must match the string criteria but col C must only match one of the values in the array given as criteria. Match on colA AND colB AND on one array value in col C.
A different approach would be to create one COUNTIFS function for each value in the array like
COUNTIFS($A:$A,"string1" , $B:$B,"string2" , $C:$C,"stringA") + COUNTIFS($A:$A,"string1" , $B:$B,"string2" , $C:$C,"stringB") + COUNTIFS($A:$A,"string1" , $B:$B,"string2" , $C:$C,"stringC")
This however is a lot of duplicate code and that bugs me! The logic solution would be to pass an array as criteria for column C. Also my array contains more then three values...
When I do this in Excel the formula is accepted and a few rows are counted but the results are way to low. It is not the result I´m expecting.
Any Excel-Pro out there that can tell me if this is possible? It would save me a lot of work!
Thanks!
you can use
=SUM(COUNTIFS($A:$A,"string1",$B:$B,"string2",$C:$C,{"stringA","stringB","stringC"}))
you can also use a similar array construction with sumproduct
=SUMPRODUCT(($A:$A="string1")*($B:$B="string2")*($C:$C={"stringA","stringB,"stringC"}))
Hmm, well, 'or' does make things longer. See a related question. I guess you could use =SUMPRODUCT() but still have it a bit long. Being an array function though, you'll have to use Ctrl + Shift + Enter.
=SUMPRODUCT(($A:$A="string1")+0,($B:$B="string2")+0,((($C:$C="stringA")+($C:$C="stringB")+($C:$C="stringC"))>0)+0)
Disclaimer: I haven't tested this yet.
Related
I am trying to get a sum of column C if A is abc or cba and B is def:
=SUMIFS(C2:C51;A2:A51;{"abc","cba"};B2:B51;"def")
But the formula is not valid, not sure where is my mistake since this was proposed in a quick google search.
Thank you for your suggestions.
The formula is valid for me, but this might be an issue with your delimiter. Depending on your excel, windows or location settings you might need to use a comma , as a delimiter, instead of a semicolon ;.
As for your formula, for completion I've done the same google search and ended up with this reference. It seems your logic in the formula is correct apart from one crucial step, the SUM( wrapping around your formula. This means if your formula works, it will only take the first hit into account, but with the sum, it will count every entry where your logic is True. Syntax:
=SUM(SUMIFS(C2:C51,A2:A51,{"abc","cba"},B2:B51,"def"))
Or semicolon delimited:
=SUM(SUMIFS(C2:C51;A2:A51;{"abc";"cba"};B2:B51;"def"))
Since the {array} option does not seem to be working for you, I propose a workaround as follows:
=SUMIFS(C1:C15;A1:A15;"abc";B1:B15;"def")+SUMIFS(C1:C15;A1:A15;"cba";B1:B15;"def")
This is a more clunky function, but reaches the same result by splitting up the data in two SUMIFS( functions and adding the results together.
Probably I would use #Plutian answer (actually I upvoted), but in case it might work for you, you can use SUMPRODUCT combined with DOUBLE UNARY to get exactly what you want.
DOUBLE UNARY
SUMPRODUCT
I made a fake dataset like this:
As you can see, only the highlighted values meet your requirements ( if A=abc OR cba AND B=def)
My formula in E10 is:
=SUMPRODUCT(--($A$2:$A$7="abc")+--($A$2:$A$7="cba");--($B$2:$B$7="def");$C$2:$C$7)
This is how it works:
($A$2:$A$7="abc") will return an array of True/False values if condition is met.
That array, because it's inside a double unary operator --( your range ), will convert all True/False values into 1 or 0 values. Let's say it works like if you would have selected a range of cells that contains only 1 or 0. So this will return an array like {1,0,1,0,1,0} in this case
--($A$2:$A$7="cba") will do exactly the same than steps 1 or 2 again, but with your second option. It will return another array of values, in this case, {0,1,0,1,0,1}
--($A$2:$A$7="abc")+--($A$2:$A$7="cba") we are just summing up both arrays, so {1,0,1,0,1,0}+{0,1,0,1,0,1}={1,1,1,1,1,1}
--($B$2:$B$7="def") will do like steps 1 and 2 again with your third condition, and will return another array, now it will be {1,0,1,0,0,1}
The array obtained in step 5 then it's multiplied to array obtained in step 4, so we are doing {1,1,1,1,1,1} * {1,0,1,0,0,1}={1,0,1,0,0,1}
Now, that final array obtained in step 7 then it's multiplied by the values of cells $C$2:$C$7, so in this case is {1,0,1,0,0,1} * {10,1,10,1,1,10} = {10,0,10,0,0,10}
And final step, we sum up all values inside array obtained in last step, so we do 10+0+10+0+0+10=30
I've explained every step to make sure everybody can understand, because SUMPRODUCT it's really an useful function if you know how to hanlde (I'm a noob, but I've seen real heroes here on SO using this function).
The advantage of using SUMPRODUCT instead of SUMIFS is that you can easily add more conditions to apply same range (case --($A$2:$A$7="abc")+--($A$2:$A$7="cba") or single condition to additional ranges (case --($B$2:$B$7="def")).
With normal SUMIFS probably you would have to add 1 extra complete SUMIF for each condition applied in same range.
Hope this helps
This question was asked and as the answer to the specific question was a typo it was deleted:
https://stackoverflow.com/questions/59289065/excel-non-adjecent-cells-as-input-to-array-function-min-and-isblank
Here is the question:
I'm trying to find the minimum of two (non-adjacent) cells per column and sum these for a number of columns (13 in total).
What complicates it is that I'd like the function to treat empty cells as zero.
I can get it to work as long as the cells are adjacent, but when they are not, excel gives a "too many arguments for this function" pop-up.
The formula I have for adjecent cells is this (not exactly pretty, sorry!):
{=SUM(MIN(IF(ISBLANK(P3:P4);0;P3:P4));MIN(IF(ISBLANK(Q3:Q4);0;Q3:Q4));MIN(IF(ISBLANK(R3:R4);0;R3:R4));MIN(IF(ISBLANK(S3:S4);0;S3:S4));MIN(IF(ISBLANK(T3:T4);0;T3:T4));MIN(IF(ISBLANK(U3:U4);0;U3:U4));MIN(IF(ISBLANK(V3:V4);0;V3:V4));MIN(IF(ISBLANK(W3:W4);0;W3:W4));MIN(IF(ISBLANK(X3:X4);0;X3:X4));MIN(IF(ISBLANK(Y3:Y4);0;Y3:Y4));MIN(IF(ISBLANK(Z3:Z4);0;Z3:Z4));MIN(IF(ISBLANK(AA3:AA4);0;AA3:AA4));MIN(IF(ISBLANK(AB3:AB4);0;AB3:AB4)))}
This gives the desired output in the column "person months total".
I have tried to use the CHOOSE function for non-adjacent cells as a test (similar to this question), but this gives the "There's something wrong with this formula" pop-up
=SUM(MIN(IF(ISBLANK(CHOOSE{1;2};P16;P18));0;CHOOSE({1;2};P16;P18)))
So now I'm wondering, can this be done at all? Am I missing something?
I would appreciate the help!
Kind regards,
Amy
While the answer to this question was that there was a missing ( after the first CHOOSE, I started working on a simpler version to that hideous long formula and wanted to post it here. So the question is, "Is there a simpler method not using vba?"
With the ranges actually being adjacent by rows, but comparing column by column we can use MMULT in an array form. By using MMULT we can create an array of the smallest numbers and 0s and sum them:
=SUM(MMULT(N(IF(A1:E1>A2:E2,IF(A2:E2<>"",A2:E2),IF(A1:E1<>"",A1:E1))),TRANSPOSE(COLUMN(A1:E1)^0)))
This is an array formula and must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
EDIT: way over thought it, this is much simpler:
=SUM(IF(A1:E1>A2:E2,IF(A2:E2<>"",A2:E2),IF(A1:E1<>"",A1:E1)))
Still an array formula.
I have a cell which needs to sum together 3 different values, but I don't want to use relative/absolute cell references because the source data changes every year so the cell references would need re-pointing every year.
I've used VLOOKUPs for cells which only need to show 1 value and that's all fine, but I cannot find a way to add together the outcomes of 2 or more VLOOKUPs. Also, each of the 3 values have different criteria, and I think I therefore need 3 separate VLOOKUP functions.
Is there a way of doing this?
This is the formula I've used, which returns a '#N/A' error:
=vlookup("10000",datatable,3,false)+vlookup("10001",datatable,3,false)+vlookup("10002"),datatable,3,false)+...etc etc etc.
Many thanks in advance
Rob
Here is a simple example of summing 3 VLOOKUPs:
=SUM(VLOOKUP("A",A1:B3,2,FALSE),VLOOKUP("E",A5:B7,2,FALSE),VLOOKUP("I",A9:B11,2,FALSE))
I put 3 simple tables, each 3 rows and 2 columns, and added the lookup values together. So I look up A and return 1, E and return 5, I and return 9. Then I sum them and return 15. :-)
Providing the only part to vary is the lookup_value (as appears to be so in your case) then, instead of writing multiple distinct VLOOKUP clauses followed by summing them (which could result in an extremely long construction indeed), you can use a single, equivalent VLOOKUP set-up.
So instead of, for example:
=VLOOKUP("X",DataTable,3,FALSE)+VLOOKUP("Y",DataTable,3,FALSE)+VLOOKUP("Z",DataTable,3,FALSE)
we can use the shorter:
=SUM(VLOOKUP(T(IF(1,{"X","Y","Z"})),DataTable,3,FALSE))
or, for more flexibility, if we have "X", "Y" and "Z" within a contiguous range somewhere within the worksheet, e.g. H1:H3, then:
=SUM(VLOOKUP(T(IF({1},H1:H3)),DataTable,3,FALSE))
If the criteria are not text strings, but numerics, then we can use N instead of T: for example, if we were looking for, not "X", "Y" and "Z", but 1, 2 and 3, then we would use:
=SUM(VLOOKUP(N(IF(1,{1,2,3})),DataTable,3,FALSE))
or:
=SUM(VLOOKUP(N(IF({1},H1:H3)),DataTable,3,FALSE))
If our criteria comprise a mixture of text and numerics, e.g. "X", 2 and "Z", then we need a slightly different approach, though I will leave that to another post!
References:
https://excelxor.com/2014/09/05/index-returning-an-array-of-values/
Regards
Good heavens. Stop using VLOOKUP, everyone. INDEXtogether with MATCH is cleaner, neater and less prone to errors.
Now, for what you are doing, you don't need either. Why not check out SUMIFS
In your case it would look something like this
=SUMIFS(Sumcolumn;Criteriacolumn;10000;Criteriacolumn2;10001,...)
And, even if your sumcolumns are different from each other - it is still easier to sum several sumifs than it is to sum several vlookups, since vlookup will throw a #N/A if empty reference, whereas sumifs will return 0. If you have a desire to be extra efficient with just one formula, use SUMPRODUCT - however that is a formula that needs some practice to write.
Does anyone have any brilliant ideas to simplify this difficult formula? Don't panic when you see it, I will try to explain.
=IFERROR(INDEX(rangeOfDesiredValues,(1/SUMPRODUCT((rangeOfSerials=$D20)(rangeOfApps=cfgAppID)(rangeOfAccessIDs=cfgAccessID)*ROW(rangeOfDesiredValues))^-1)),"")
Currently I am using SUMPRODUCT to do the equivalent of a VLOOKUP with multiple columns as criteria. Usually that only works with number results, but since I need to find text, I'm using SUMPRODUCT in combination with ROW and INDEX.
Unfortunately when no cell is found, my SUMPRODUCT returns 0. This causes the formula to return the incorrect cell rather than blank. For this reason I am running the result through this calculation:
(1 / result)^-1
This way results of 0 become an error, and other results remain unchanged. I feed this into IFERROR, so that errors become blanks.
Does anyone know how to make this neater? I am not able to create new columns in any of my spreadsheets.
It's always best to avoid using multi-condition summing functions like SUMPRODUCT when you want to find a single value (it would obviously give you an incorrect result or error if there's more than one row which matches all three conditions, I assume you expect one match at most here?). ROW function can also be problematic if you insert any rows in the worksheet.....
There are several approaches that can work. For a single formula, using MATCH is the most common - MATCH will only give the correct position or an error so no problems with zero values. That would look like this:
=IFERROR(INDEX(rangeOfDesiredValues,MATCH(1,(rangeOfSerials=$D20)*(rangeOfApps=cfgAppID)*(rangeOfAccessIDs=cfgAccessID),0)),"")
That's an "array formula" that needs to be entered with CTRL+SHIFT+ENTER......or you can make it into a regular formula with an extra INDEX function like this
=IFERROR(INDEX(rangeOfDesiredValues,MATCH(1,INDEX((rangeOfSerials=$D20)*(rangeOfApps=cfgAppID)*(rangeOfAccessIDs=cfgAccessID),0),0)),"")
A third alternative is to use LOOKUP which doesn't need "array entry"
=IFERROR(LOOKUP(2,1/(rangeOfSerials=$D20)/(rangeOfApps=cfgAppID)/(rangeOfAccessIDs=cfgAccessID),rangeOfDesiredValues),"")
That differs slightly from the previous versions in the case of multiple matches - it will give you the last match rather than the first in that scenario (but I assume you have only one match at most, as stated above).
Finally, if you don't mind using helper columns you could simplify the formulas considerably. Just use a "helper" column to concatenate the three criteria columns separated by dashes and then you can use a simple VLOOKUP or INDEX/MATCH, e.g.
=IFERROR(INDEX(rangeOfDesiredValues,MATCH($D20&"-"&cfgAppID&"-"&cfgAccessID,Helper_Column,0)),"")
This follows on from my 2 previous questions: Excel Match multiple criteria and Excel find cells from range where search value is within the cell. Apologies if I'm over-posting but I figured each question is slightly different and bothering people for follow-up questions doesn't seem fair if I can't mark their answer as correct.
I have this working piece of code which checks 3 columns of data to return a value from range1 if all of the criteria are met:
=INDEX(range1,MATCH(1,(A2=range2)*(B2=range3)*(SEARCH(range4,J2)),0))
However I need the SEARCH option to work if a match is found in range4 OR range5, and thanks to John Bustos and Barry Houdini from this site I know how to use an OR command within a MATCH Function:
=INDEX(range1,MATCH(1,(A2=range2)*(B2=range3)*(((C2 = range4)+(D2 = range5))>0),0))
The code above works for exact matches but the Values of C2 and D2 are lists of numbers contained in single cells, and range4 and range5 are single years in each cell so the SEARCH function has to be used to check whether the single years are present within the list of years. So, judging by the two working pieces of code above I thought this would work:
=INDEX(range1,MATCH(1,(A2=range2)*(B2=range3)*(((SEARCH(range4,J2))+(SEARCH(range5,J2))>0)),0))
However, it does not, and nor does:
=INDEX(range1,MATCH(1,(A2=range2)*(B2=range3)*(((SEARCH(range4,J2))+(SEARCH(range5,J2)))),0))
I am remembering to Press CTRL+SHIFT+ENTER, it just always returns #N/A. I know it should be returning values as the first example works before I try and make it into an OR command.
I hope someone can shed some light on this. Thanks in advance,
Best Wishes,
Joe
The problem you get in changing to SEARCH is that with C2=range4 in previous version of the formula the result of that is either TRUE or FALSE whereas SEARCH(range4,J2) returns a number (position of range 4 value in J2) or a VALUE! error if it isn't in J2
...so if only one SEARCH finds a value and the other doesn't you'll still get the error generated by the one that doesn't and MATCH won't get a match, so the "OR" doesn't work........to fix that you need to add something to get the SEARCH to return TRUE or FALSE - you can do that with ISNUMBER, i.e.
=INDEX(range1,MATCH(1,(A2=range2)*(B2=range3)*(ISNUMBER(SEARCH(range4,J2))+ISNUMBER(SEARCH(range5,J2))>0),0))
Note also that the first formula you quote:
=INDEX(range1,MATCH(1,(A2=range2)*(B2=range3)*(SEARCH(range4,J2)),0))
may not work as intended because SEARCH might return a number other than 1, so you need ISNUMBER there as well, i.e.
=INDEX(range1,MATCH(1,(A2=range2)*(B2=range3)*ISNUMBER(SEARCH(range4,J2)),0))
For array formulas isnt it "Ctrl + Shift + Enter". Try that and see if it does anything.