简体   繁体   中英

Excel formula: counting array elements with a criteria

I have an array, which is the result of the multiplication of two COUNTIF functions.

The result is {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

It did not work. I then tried the following, which did not work either.

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

Two questions:

  1. does COUNTIF only accept cell reference but not constant array?
  2. any solution other than array formula?

Thanks.

  1. The first argument of COUNTIF must be a range, nothing else will do.

  2. You can just enter this as an ordinary formula:

    =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))

or like @Jeeped with Sumproduct

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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