How to count rows between 2 non empty cells with their intermediary empty cells in Google Sheets? - google-sheets

I'm trying to count each rows of empty cells between two non-empy cells and adding the upper non empty-cell count (or the lower non-empty cells count) to the result when the empty cells/rows numbers are variable and irregular in this manner:
A1 Empty
A2 Non-empty
A3 Empty
A4 Empty
A5 Empty
A6 Empty
A7 Non-empty
(with A2 to A5 being irregular — could be any number of intermediary empty rows/cells — i.e. 10, 2, 30, 5, etc., and variable — could be located in any location within Column A)
The result should return as follow:
=IFS(
AND(A2="",A3=""),"",
AND(A2="",A3<>""),"",
AND(A2<>"",A3<>""),ROWS(A2),
AND(A2<>"",A3=""),ROWS(A2:UP TO NEXT NON-EMPTY ROW BENEATH (NOT COMPRISED (or minus the next non-empty row(-1)))
How do we program Google Sheets to count rows "UP TO NEXT NON-EMPTY ROW BENEATH (NOT COMPRISED (or minus the next ono empty row(-1)) when the emppty intermediary rows' count is variable/unknown for each occurence and irregular/can be any number of intremediary rows?
I have this data as example:
So far, I've come up with this partial solution:
=IFS(AND(A2="",A3=""),"", AND(A2="",A3<>""),"", AND(A2<>"",A3<>""),ROWS(A2), AND(A2<>"",A3=""),ROWS(A2:UP TO NEXT NON-EMPTY ROW BENEATH (NOT COMPRISED (or minus the next non-empty row(-1)))))
The expected result should return as follows:
The Sheet copy:
https://docs.google.com/spreadsheets/d/1eLXdFINKVtS5Fxon8mIQDmBUTZokyY_Fb6tfWJRDOVI/edit?usp=sharing

Well I think you can do it a bit more elegantly using a reverse lookup. The idea is to sort the data on descending order of row number, then use a match with -1 as the last parameter, which is equivalent to searching from the end of the data for the next non-blank.
=ArrayFormula(iferror(if(A2:A="","",rows(A2:A)+2-match(row(A2:A)+1,
query(SORT({IF(A2:A<>"",row(A2:A),""),row(A2:A)},2,false),"select Col1"),-1)-row(A2:A)),1))
If you wanted to keep it simple and just use a pull-down formula, an ordinary match is enough:
=ArrayFormula(iferror(if(A2="","",match(true,A3:A<>"",0)),1))
(it still has to be entered as an array formula because A3:A<>"" is an array of True and False)

I found a solution with a Monster formula but with simple iterative logics, and up to 50 intermediary blanks (for more only need to make the formula larger).
What I was lookind for was simply to print the cells/rows count of intermediary empty cells between 2 non-empty cells + adding to the count either the 1st non-empty cell or the last non-empty cell.
So for example if cell A1 and cell A9 have 8 empty cells between them, then return 8+1 (or 10-1).
The logics used is simple:
probe how many possible empty cells there can be between each 2 non-empty cells counting the 1st non-empty as well within column A (in the formula below the possibility is up to 50 empty cells), and for each case return the intermediary rows -1 (the last row).
The formula:
=IFS(AND(A1="",A2=""),"",AND(A1="",A2<>""),"",AND(A1<>"",A2<>""),ROWS(A1),AND(A1<>"",A2="",A3<>""),ROWS(A1:A3)-1,AND(A1<>"",A2="",A3="",A4<>""),ROWS(A1:A4)-1,AND(A1<>"",A2="",A3="",A4="",A5<>""),ROWS(A1:A5)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6<>""),ROWS(A1:A6)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7<>""),ROWS(A1:A7)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8<>""),ROWS(A1:A8)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9<>""),ROWS(A1:A9)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10<>""),ROWS(A1:A10)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11<>""),ROWS(A1:A11)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12<>""),ROWS(A1:A12)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13<>""),ROWS(A1:A13)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14<>""),ROWS(A1:A14)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15<>""),ROWS(A1:A15)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16<>""),ROWS(A1:A16)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17<>""),ROWS(A1:A17)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18<>""),ROWS(A1:A18)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19<>""),ROWS(A1:A19)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20<>""),ROWS(A1:A20)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21<>""),ROWS(A1:A21)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22<>""),ROWS(A1:A22)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23<>""),ROWS(A1:A23)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24<>""),ROWS(A1:A24)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25<>""),ROWS(A1:A25)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26<>""),ROWS(A1:A26)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27<>""),ROWS(A1:A27)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28<>""),ROWS(A1:A28)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29<>""),ROWS(A1:A29)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30<>""),ROWS(A1:A30)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31<>""),ROWS(A1:A31)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32<>""),ROWS(A1:A32)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33<>""),ROWS(A1:A33)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34<>""),ROWS(A1:A34)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35<>""),ROWS(A1:A35)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36<>""),ROWS(A1:A36)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37<>""),ROWS(A1:A37)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38<>""),ROWS(A1:A38)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39<>""),ROWS(A1:A39)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40<>""),ROWS(A1:A40)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41<>""),ROWS(A1:A41)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42<>""),ROWS(A1:A42)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43<>""),ROWS(A1:A43)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44<>""),ROWS(A1:A44)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45<>""),ROWS(A1:A45)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46<>""),ROWS(A1:A46)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47<>""),ROWS(A1:A47)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47="",A48<>""),ROWS(A1:A48)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47="",A48="",A49<>""),ROWS(A1:A49)-1,AND(A1<>"",A2="",A3="",A4="",A5="",A6="",A7="",A8="",A9="",A10="",A11="",A12="",A13="",A14="",A15="",A16="",A17="",A18="",A19="",A20="",A21="",A22="",A23="",A24="",A25="",A26="",A27="",A28="",A29="",A30="",A31="",A32="",A33="",A34="",A35="",A36="",A37="",A38="",A39="",A40="",A41="",A42="",A43="",A44="",A45="",A46="",A47="",A48="",A49="",A50<>""),ROWS(A1:A50)-1)
Here the images sheet of the result:
The Sheet
Can it be done in a simpler way but still with a single formula?
Thanks a lot for your next input!

In the mean time I did some simplification of my previous Monster formula and learned the ArrayFormula application in the process.
1st I tried to shorten the big blank cells intervals into ranges formulations to see if it still worked.
For example I changed:
AND(A1<>"",A2="",A3="",A4<>""),ROWS(A1:A4)-1,
into
AND(A1<>"",A2:A3="",A4<>""),ROWS(A1:A4)-1,
And it shortened my formula from this:
to this:
But then when pasting the new formula I got a #VALUE! error 'an array value could not be found'.
After further tests, I used the ArrayFormula and it worked by adapting the simple 'ranges' previously used to Arrays:
from this
AND(A1<>"",A2:A3="",A4<>""),ROWS(A1:A4)-1,
into this
AND(A1<>"",ArrayFormula(A2:A3=""),A4<>""),ROWS(A1:A4)-1,
With this final result:
=IFS(AND(A1="",A2=""),"",AND(A1="",A2<>""),"",AND(A1<>"",A2<>""),ROWS(A1),AND(A1<>"",A2="",A3<>""),ROWS(A1:A3)-1,AND(A1<>"",ArrayFormula(A2:A3=""),A4<>""),ROWS(A1:A4)-1,AND(A1<>"",ArrayFormula(A2:A4=""),A5<>""),ROWS(A1:A5)-1,AND(A1<>"",ArrayFormula(A2:A5=""),A6<>""),ROWS(A1:A6)-1,AND(A1<>"",ArrayFormula(A2:A6=""),A7<>""),ROWS(A1:A7)-1,AND(A1<>"",ArrayFormula(A2:A7=""),A8<>""),ROWS(A1:A8)-1,AND(A1<>"",ArrayFormula(A2:A8=""),A9<>""),ROWS(A1:A9)-1,AND(A1<>"",ArrayFormula(A2:A9=""),A10<>""),ROWS(A1:A10)-1,AND(A1<>"",ArrayFormula(A2:A10=""),A11<>""),ROWS(A1:A11)-1,AND(A1<>"",ArrayFormula(A2:A11=""),A12<>""),ROWS(A1:A12)-1,AND(A1<>"",ArrayFormula(A2:A12=""),A13<>""),ROWS(A1:A13)-1,AND(A1<>"",ArrayFormula(A2:A13=""),A14<>""),ROWS(A1:A14)-1,AND(A1<>"",ArrayFormula(A2:A14=""),A15<>""),ROWS(A1:A15)-1,AND(A1<>"",ArrayFormula(A2:A15=""),A16<>""),ROWS(A1:A16)-1,AND(A1<>"",ArrayFormula(A2:A16=""),A17<>""),ROWS(A1:A17)-1,AND(A1<>"",ArrayFormula(A2:A17=""),A18<>""),ROWS(A1:A18)-1,AND(A1<>"",ArrayFormula(A2:A18=""),A19<>""),ROWS(A1:A19)-1,AND(A1<>"",ArrayFormula(A2:A19=""),A20<>""),ROWS(A1:A20)-1,AND(A1<>"",ArrayFormula(A2:A20=""),A21<>""),ROWS(A1:A21)-1,AND(A1<>"",ArrayFormula(A2:A21=""),A22<>""),ROWS(A1:A22)-1,AND(A1<>"",ArrayFormula(A2:A22=""),A23<>""),ROWS(A1:A23)-1,AND(A1<>"",ArrayFormula(A2:A23=""),A24<>""),ROWS(A1:A24)-1,AND(A1<>"",ArrayFormula(A2:A24=""),A25<>""),ROWS(A1:A25)-1,AND(A1<>"",ArrayFormula(A2:A25=""),A26<>""),ROWS(A1:A26)-1,AND(A1<>"",ArrayFormula(A2:A26=""),A27<>""),ROWS(A1:A27)-1,AND(A1<>"",ArrayFormula(A2:A27=""),A28<>""),ROWS(A1:A28)-1,AND(A1<>"",ArrayFormula(A2:A28=""),A29<>""),ROWS(A1:A29)-1,AND(A1<>"",ArrayFormula(A2:A29=""),A30<>""),ROWS(A1:A30)-1,AND(A1<>"",ArrayFormula(A2:A30=""),A31<>""),ROWS(A1:A31)-1,AND(A1<>"",ArrayFormula(A2:A31=""),A32<>""),ROWS(A1:A32)-1,AND(A1<>"",ArrayFormula(A2:A32=""),A33<>""),ROWS(A1:A33)-1,AND(A1<>"",ArrayFormula(A2:A33=""),A34<>""),ROWS(A1:A34)-1,AND(A1<>"",ArrayFormula(A2:A34=""),A35<>""),ROWS(A1:A35)-1,AND(A1<>"",ArrayFormula(A2:A35=""),A36<>""),ROWS(A1:A36)-1,AND(A1<>"",ArrayFormula(A2:A36=""),A37<>""),ROWS(A1:A37)-1,AND(A1<>"",ArrayFormula(A2:A37=""),A38<>""),ROWS(A1:A38)-1,AND(A1<>"",ArrayFormula(A2:A38=""),A39<>""),ROWS(A1:A39)-1,AND(A1<>"",ArrayFormula(A2:A39=""),A40<>""),ROWS(A1:A40)-1,AND(A1<>"",ArrayFormula(A2:A40=""),A41<>""),ROWS(A1:A41)-1,AND(A1<>"",ArrayFormula(A2:A41=""),A42<>""),ROWS(A1:A42)-1,AND(A1<>"",ArrayFormula(A2:A42=""),A43<>""),ROWS(A1:A43)-1,AND(A1<>"",ArrayFormula(A2:A43=""),A44<>""),ROWS(A1:A44)-1,AND(A1<>"",ArrayFormula(A2:A44=""),A45<>""),ROWS(A1:A45)-1,AND(A1<>"",ArrayFormula(A2:A45=""),A46<>""),ROWS(A1:A46)-1,AND(A1<>"",ArrayFormula(A2:A46=""),A47<>""),ROWS(A1:A47)-1,AND(A1<>"",ArrayFormula(A2:A47=""),A48<>""),ROWS(A1:A48)-1, AND(A1<>"",ArrayFormula(A2:A48=""),A49<>""),ROWS(A1:A49)-1, AND(A1<>"",ArrayFormula(A2:A49=""),A50<>""),ROWS(A1:A50)-1)
New Sheet here

Related

how could I count minus cells in a column excluding the last one?

I want to count on another sheet that how many people have a minus balance. Of course, I should use COUNTIF, but how could I dynamically narrow the range from C2 to the second last non-blank cell(because there's a chance for the total balance to be minus, it should be excluded)?
Try below formula-
=COUNTIFS(C2:INDEX(C2:C,COUNTA(C2:C)-1),"<0")
Here C2:INDEX(C2:C,COUNTA(C2:C)-1) will return a array of values as well cell reference from C2 to last second non empty cell in column C (Assume you do not have any blank rows inside data). If you have blank row, then you have to use different approach. See this post by #TheMaster
COUNTA(C2:C) will count all non blank cells in C column then minus 1 from that count to make it second non empty cell till down to up.
And then apply COUNTIFS() function.

How to sum numbers within text in google sheets?

Im looking to sum the numbers within a cell of text, into another cell.
Example of Excel
Try
=sum(split(join("|",REGEXEXTRACT(A1,REGEXREPLACE(A1,"([0-9]+)","($1)"))),"|"))
Clear Col C entirely (including the header "TOTAL"). Then place the following formula in cell C1:
=ArrayFormula({"TOTAL"; MMULT(ARRAY_CONSTRAIN(SPLIT(REGEXREPLACE(A2:A,"[^\d]+","~")&REPT("~0",25),"~"),COUNTA(A2:A),25),SEQUENCE(25,1,1,0))})
You can change the header text within the formula itself if you like.
This one formula will produce the header and all results for all rows in Col C.
How It Works
REGEXREPLACE replaces any groupings of anything other than digits with a single tilde (~). To this new string, REPT adds a repeated string of 25 instances of ~0.
SPLIT then splits this at every tilde, which sends each number (and zero appended by REPT into its own column. Some rows at this point will have more columns than others, because you have a different numbers of listed items in each of your A2:A cells.
ARRAY_CONSTRAIN constrains the results into a regular grid with as many rows as there are non-null entries in A2:A and 25 columns. Based on your data samples, this should be more columns than you'd ever have listed items per cell in A2:A. Those REPT zeros will fill in all columns after your last actual value in A2:A.
MMULT can now act on the constrained grid, along with SEQUENCE to do row-by-row addition.

Is there any solution to left the cell empty

i have a sheet along with this question,the formula used in column E2 is : if(and(d2>=0,d2<=2),5. So when the column is blank it gives the value 5 My query is can we left the "E" column blank when there is no value in "D". ??? 5 must displayed only if there score between 0 & 2.
https://docs.google.com/spreadsheets/d/1XpdXcWDReB8TGvZ6ocALAilVPLDKzZXvd90YhNos0Io/edit?usp=sharing
Iker. I've added a sheet with two approaches.
If you want to drag the formula, just set an initial IF that rules out blanks like this:
=if(D2="","",if(and(D2>=0,D2<=2),5,if(and(D2>=3,D2<=4),0)))
I placed this into my sheet, E2, and dragged down as you were doing.
However, this is a great example of where array formulas are handy. An array formula can "run" your whole column from just one cell. I placed the following array formula into cell I1 of my sheet:
=ArrayFormula({"POINT";IF(H2:H="","",IF((H2:H>=0)*(H2:H<=2),5,IF((H2:H>=3)*(H2:H<=4),0,"")))})
First, keep in mind that array formulas must have unused space below them in order to fill in results. If you type anything manually below an array formula, inside the range it is trying to work on, you'll get an error. If you do want to put other data below an array formula, just limit the range in the array formula (e.g., change every H2:H to H2:H6 or whatever the end of that data range might be).
ArrayFormula() tells Google Sheets to apply this formula to the entire range in the formula. Since the range in the formula is H2:H, every cell in I2:I will be "reserved" by this array formula.
The curly brackets {} allow us to build another custom array inside the first array.
Since I want a header in I1, I put that header name first in the curly brackets. The semicolon tells the array to put the next part underneath.
The next part may look strange, but it's basically the same as your original formula, except that AND(), OR(), etc., don't work inside arrays. So the logical operators inside arrays are different. In this case, an asterisk * means AND.
The same conditions from your original formula are used here. And if it meets none of those criteria (for instance, if someone entered -1 or 7 or M somewhere in Column H, the last part of the last IF would just assign a blank.
You can use an added IF to your formula
=IF(D2="","", if(and(D2>=0,D2<=2),5,if(and(D2>=3,D2<=4),0)))

How to count groups of cells with specific first and last characters wrapping those groups in Google Sheets?

I'm trying to count how many cells are included in specific groups of cells with specific first and last character as counting criteria.
With separate counts per group.
Here's what the end result should look like:
So that for each group of cells beginning and ending with the character "#" as first and last character,
the result should sum the cells/rows from the 1st cell to the last cell of that group.
So that for each group of cells beginning and ending with the character "^" as first and last character,
the result should sum the cells/rows from the 1st cell to the last cell of that group.
But the formula should exclude from count any group of cells not beginning and ending with the characters "^" or "#" as first and last character.
And ideally, display the sum result on the last cell of each group to the left (as in the B column on the above screenshot).
Up to now, I've only come up with the =ROWS() function to manually count the cells of those groups. But it is tedious.
For example, to deal with the screenshot example I would do:
=ROWS(C3:C5) (with 3 as result in cell B5)
Then
=ROWS(C8:C10) (with 3 as result in cell B10)
Nothing in cell B15
Then
=ROWS(C23:C25) (with 3 as result in cell B25)
Etc.
How to get these results with a single formula over the whole B column?
In the meantime I've found this post:
Sum vertically until empty cell on Google Sheets
Which answers partially the count part of my post.
It got me thinking and I figured these 3 steps.
1st step: count all the cells with len.
=IF(len(C2),1,"")
2nd Step: Adapt the formula:
IF(C2="",SUM(C3:INDEX(C3:C,MATCH(TRUE,(C3:C=""),0))),"")
Found on the post above, to this (taking into account more than one intermediary blank row (D1, D2, D3, D4, etc):
=IFS(OR(AND(D1="",D2="",D3="",D4=""),AND(D1="",D2="",D3=""),AND(D1="",D2=""),D1=""),SUM(D2:INDEX(D2:D,MATCH(TRUE,(D2:D=""),0))),TRUE,"")
Step 3: Moving down the step 2 formula into cell F4 and dragging down from there to have the results at the last cell of each groups (instead of on the cell above each group as with the original formula location on E2)
Remaining questions:
How to specify the sum function to operate on the specific characters ("#" and "^" on the example) basis instead of on the len Function of cell criteria?
How to change the zero cells results to blanks?
How to combine the 2/3 steps formulas into a single step formula?
Here's a copy of the spreadsheet if need be:
4:How to count groups of cells with specific first and last characters wrapping those groups in Google Sheets?
Alternatively, following this post's answer by #shawnrad:
Count rows with not empty value
I used the LEFT and RIGHT functions here:
=IF((OR(LEFT(C1,1)="#",RIGHT(C1,1)="#",LEFT(C1,1)="^",RIGHT(C1,1)="^")),1,"")
to come to these results:
=IF(OR(COUNTIF(C2,"#*"),COUNTIF(C2,"*#"),COUNTIF(C2,"^*"),COUNTIF(C2,"*^")),1,"")
How to then apply the SUM function or any other function to sum the cells/rows per intervals between (and including) the returned results of the last function?

How to use ARRAYFORMULA with OFFSET to previous row without getting circular reference error

Example sheet: https://docs.google.com/spreadsheets/d/14ma-y3esh1S_EkzHpFBvLb0GzDZZiDsSVXFktH3Rr_E/edit?usp=sharing
In column B of ItemData sheet, I have achieved the result I want by copying the formula into every cell in the column, but I want to solve this using ArrayFormula instead.
In column C I have achieved the same result using ArrayFormula. However, for addition, column C is referring to cells in column B, while column B is referring to cells in column B. I.e. every cell in column B is adding 1 to the cell on the row above.
If I select the C3 formula text and paste it into the cell edit field for cell B3 (to not screw up cell references during copy - I know I could make them static references, but this is not my problem), the cell gets an error value of
#REF!
Error
Circular dependency detected. To resolve with iterative calculation, see File > Spreadsheet Settings.
Do note that the additions that need to be done are the same in both cases: Add 1 to the value of the cell on the previous row, so there is no circular reference involved. There is a starting value provided in B2, and cells in B3 and downwards should use the data from the B cell in the previous row.
Also, note that I did try File->Spreadsheet settings and enabling circular reference computation with max 25 items, but this only fills in the first two cells (B3 and B4).
How can I solve this problem? I would prefer having something like ArrayFormula, where the formula only exists in a single cell. But copy-pasting would be acceptable as long as any new rows, inserted in between or added at the bottom, would get the same formula added in column B.
Will matching items always be consecutive? It seems that way since you're comparing each Item cell to the cell above it right in your formula logic. That breaks an [unwritten?] rule of spreadsheet normalization; values' addresses themselves generally should not be treated as data.
IF you're committed to it though, have you considered explicitly using location as a data source? Example:
=ARRAYFORMULA(IFS(
NOT(LEN(A3:A40)),,
ROW(A3:A40)-3-MATCH(A3:A40,A$3:A$40,0)<=VLOOKUP(VLOOKUP(A3:A40,Items!$A$2:$D,2,false),DataPerColor!$A$2:$B,2,false),ROW(A3:A40)-3-MATCH(A3:A40,A$3:A$40,0),
true,
))
Just like your formulas, all that does in English is:
for each row,
if there's no Item, don't output any ItemData,
if the number that belongs in this cell¹ is less than or equal to the lookup, print it,
otherwise, don't output any ItemData
But then what is ¹ "the number that belongs in this cell" and how can we calculate it without using column B? I abuse locations of things to get it. Looking down your row B, each number that appears is just:
this row's number,  minus  
the row where items start [always 3],  minus  
the row number [in just the Item rows] of the first row containing this row's Item
Using the second-to-last ItemC as an example: the first ItemC is the 16th item listing, and the one we're looking up… the "second-to-last ItemC" is in row 21 of the sheet. 21-3-16 = 2 …the number you wanted.
If you can stomach that, it's a single formula and does work according to your specifications.

Resources