简体   繁体   English

Excel COUNT()vs COUNTIF()与数组

[英]Excel COUNT() vs COUNTIF() with arrays

I think this should be a straightforward question, but for some reason I can't find a solution anywhere. 我认为这应该是一个简单的问题,但由于某种原因,我无法在任何地方找到解决方案。

I have a lengthy formula in excel that, ultimately, returns an array of four items -- ie {1,2,0,0}. 我在excel中有一个冗长的公式,最终会返回一个由四个项组成的数组 - 即{1,2,0,0}。 I want to count how many of the resulting numbers are greater than zero. 我想计算结果数中有多少大于零。

When I use =COUNT({1,2,0,0}) on this result, I get the expected answer of 4. But when I try to use =COUNTIF({1,2,0,0}, ">0") it pops up with an error saying that my formula has a problem. 当我在这个结果上使用=COUNT({1,2,0,0})时,我得到了预期的答案4.但是当我尝试使用=COUNTIF({1,2,0,0}, ">0")它弹出一个错误,说我的公式有问题。

Is there something I'm doing wrong? 有什么我做错了吗? Is there an array equivalent for COUNTIF() ? COUNTIF()是否有等效的数组?

It appears the COUNTIF function only works on ranges, while the COUNT function can utilize an array. 看来COUNTIF函数仅适用于范围,而COUNT函数可以使用数组。

Give SUMPRODUCT a try. 尝试一下SUMPRODUCT Below is a slightly expanded form of your example which I used to test the formula. 下面是我用来测试公式的示例的略微扩展形式。 It basically checks to see if each value in the array is greater than 0, and if it is, it assigns it a value of 1. Then SUMPRODUCT goes through and adds up all the 1s to give you the total number of values greater than 0. 它基本上检查数组中的每个值是否大于0,如果是,则为其赋值1.然后SUMPRODUCT通过并将所有1加起来给出大于0的值的总数。

=SUMPRODUCT(IF({1,0,3,0,5,0,0,6,9,9,0,7,0}>0,1,0))

Probably the most concise way to accomplish this is to just convert the TRUE or FALSE value returned from the validation check into a number with the INT function. 可能最简洁的方法是将验证检查返回的TRUEFALSE值转换为INT函数的数字。 TRUE translates to 1 and FALSE translates to 0 . TRUE转换为1FALSE转换为0 Then SUM those 1's and 0's. 然后SUM的1和0。

=SUM(INT({1,2,0,0}>0))

Or as Barry Houdini points out, you can coerce the boolean to an int with: 或者正如Barry Houdini指出的那样,您可以使用以下方法将布尔值强制转换为int:

=SUM(({1,2,0,0}>0)*1)

Or: 要么:

=SUM(({1,2,0,0}>0)+0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM