Search code examples
arraysgoogle-sheetscountformulaarray-formulas

Google sheets - Numerate rows between values in arrayformula


I have an arrayformula that numbers "chapters" based on finding the word "foo" in a range. I need it to also number the "sub-chapters" of those chapters, starting at 1 and resetting for each chapter.

A bit hard to explain but it's like this:

What I have
1 foo


2 foo



3 foo
What I need
1 foo
1
2
2 foo
1
2
3
3 foo

The arrayformula I have found works so far is this:

=ARRAYFORMULA(IF(C:C="foo",COUNTIFS(ROW(C:C),"<="&ROW(C:C),C:C,"foo"),""))

https://docs.google.com/spreadsheets/d/1ckKTsbJ7xV0SCA7tUZ8J2hB30YpTByIBPDk9OKBE_AU/edit?usp=sharing


Solution

  • use:

    =ARRAYFORMULA(ARRAY_CONSTRAIN({IF(B2:B="", COUNTIFS(
     VLOOKUP(ROW(B2:B), IF(B2:B<>"", {ROW(B2:B), B2:B}), 2, 1), 
     VLOOKUP(ROW(B2:B), IF(B2:B<>"", {ROW(B2:B), B2:B}), 2, 1), 
     ROW(B2:B), "<="&ROW(B2:B))-1, B2:B), C2:C}, 
     MAX((B2:B<>"")*(ROW(B2:B)))-1, 2))
    

    enter image description here