简体   繁体   中英

Excel count number of cells in column with “an” until next cell is non“an”

I would like to count the number of cells in column with "an", where the counting starts at a given row and counts go down to count the number of cells with "an" until a non-"an" cell is found?

For example

Column A
opp
an 
an 
an
an
opp

I would like to calculate how many cell with "an" until the next non an cell. For this example answer will be 4.

Another example

Column B:
an 
an 
opp 
an 
an
an

The answer will be 2.

You can perhaps try the formula:

=SUM(MATCH(0,IF(OFFSET(A1:A7,MATCH("an",A1:A7,0)-1,0)="an",1,0),0))-1

Entered as an array formula using Ctrl + Shift + Enter

Where A1:A7 is the range. Change it accordingly.


EDIT: To count from the bottom, maybe this:

=COUNTIF(OFFSET(C1:C9,MATCH(2,IF(OFFSET(C1,0,0,MATCH(2,IF(C1:C9<>"an",NA(),1)))="an",NA(),1)),0),"an")

Also entered as an array formula using Ctrl + Shift + Enter

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