Google Spreadsheets:如何将COUNTIF与OR结合使用

时间:2014-01-29 00:09:34

标签: google-sheets formula countif

在Google Spreadsheets中,我需要在具有多个条件的范围内使用COUNTIF函数。所以在下表中我需要有类似= COUNTIF(B:B,“Mammal”或“Bird”)的东西,并返回值为4。

A         |B
-------------------
Animal    | Type
-------------------
Dog       | Mammal
Cat       | Mammal
Lizard    | Reptile
Snake     | Reptile
Alligator | Reptile
Dove      | Bird
Chicken   | Bird

我尝试过很多不同的方法而没有运气。任何帮助表示感谢 - 谢谢。

4 个答案:

答案 0 :(得分:19)

一个选项:

=COUNTIF(B:B; "Mammal") + COUNTIF(B:B; "Bird")

根据文件:

  

备注

     

COUNTIF只能使用单一条件执行条件计数。   要使用多个条件,请使用COUNTIFS或数据库函数   DCOUNTDCOUNTA

COUNTIFS:此功能仅适用于new Google Sheets

示例:

=DCOUNTA(B:B; 2; {"Type"; "Mammal"; "Bird"})

答案 1 :(得分:2)

您还可以在SUM(COUNTIFS())构造周围使用ArrayFormula:

=ArrayFormula(SUM(COUNTIF(B:B,{"Mammal", "Bird"}))

Source: Google Docs Product Forum

答案 2 :(得分:1)

您可以像这样使用正则表达式:

=ARRAYFORMULA(SUM(N(REGEXMATCH(B:B, "Mammal|Bird"))))

0

答案 3 :(得分:0)

正如艾哈迈德·兹亚德(Ahmed Ziyad)指出的那样,COUNTIFS无效:

=COUNTIFS(B:B, "Mammal", B:B, "Bird")

在所有条件都为真(AND),而不是任何(OR)条件为真的情况下,它将计算其合并范围内的行。