Array as criteria in Excels COUNTIFS function, mixing AND and OR [duplicate]

被刻印的时光 ゝ 提交于 2020-01-02 18:05:50

问题


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!


回答1:


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"}))



回答2:


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.



来源:https://stackoverflow.com/questions/17169525/array-as-criteria-in-excels-countifs-function-mixing-and-and-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!