简体   繁体   中英

Counting how many cells have a higher value than the cell below them (COUNTIF and OFFSET)

I am trying to count how many cells in a particular column have a higher value than the cell below them. This is how far I've gotten, but it doesn't work (it always returns 0, even if there is at least one cell bigger than the one below it):

=COUNTIF(B4:B500,">"&OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),1,0))

Background (in case anyone is wondering): I am using this to check if the values in this column are properly sorted (ie starting with the lowest value and moving down to the highest). If the formula returns a value greater than 1, I know that the list is not fully sorted.

Use the SUMPRODUCT:

=SUMPRODUCT(1*(B4:B16>B5:B17))

![在此处输入图片描述

you could also use an array formula .

{=SUM(N(A1:A13<A2:A14))}

which will check your logical condition for each cell in the array, then convert it to either 0 or 1 by using N() function and then sum it up. i prefer this approach because, unlike SUMPRODUCT() , you can use it with other functions than SUM .

please remember that after inserting an array formula into a cell, you must confirm it by pressing CTRL+SHIFT+ENTER .

屏幕截图

=COUNTIF(B5:B16,">"&B4) then copy

在此处输入图片说明

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