简体   繁体   English

Excel公式:使用条件对数组元素进行计数

[英]Excel formula: counting array elements with a criteria

I have an array, which is the result of the multiplication of two COUNTIF functions. 我有一个数组,这是两个COUNTIF函数相乘的结果。

The result is {0,2,7,4,0}. 结果为{0,2,7,4,0}。 I want to count the number of non-zero elements. 我想计算非零元素的数量。 I tried the following: 我尝试了以下方法:

=COUNTIF(COUNTIF*COUNTIF,">0") <- here the two inner COUNTIFs are short for the complete formula = COUNTIF(COUNTIF * COUNTIF,“> 0”)<-这里的两个内部COUNTIF是完整公式的缩写

It did not work. 这没用。 I then tried the following, which did not work either. 然后,我尝试了以下方法,这些方法也不起作用。

=COUNTIF({0,2,7,4,0},">0") = COUNTIF({0,2,7,4,0},“> 0”)

I resorted to using the following array formula and it worked. 我求助于使用以下数组公式,并且有效。

{=SUM(IF(COUNTIF*COUNTIF>0,1,0))} {= SUM(IF(COUNTIF * COUNTIF> 0,1,0))}

Two questions: 两个问题:

  1. does COUNTIF only accept cell reference but not constant array? COUNTIF只接受单元格引用而不接受常量数组吗?
  2. any solution other than array formula? 除了数组公式,还有其他解决方案吗?

Thanks. 谢谢。

  1. The first argument of COUNTIF must be a range, nothing else will do. COUNTIF的第一个参数必须是一个范围,其他任何操作都不会做。

  2. You can just enter this as an ordinary formula: 您可以将其作为普通公式输入:

    =SUM(--({0,2,7,4,0}>0)) = SUM(-({0,2,7,4,0}> 0))

  3. However when the array comes from a calculation, you need to coerce it to process all the elements either 但是,当数组来自计算时,您需要强制其处理所有元素

    =SUM(INDEX(--(A1:A5>0),0)) = SUM(INDEX(-(A1:A5> 0),0))

or like @Jeeped with Sumproduct 或像@Jeeped with Sumproduct

=SUMPRODUCT(--(A1:A5>0))

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

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