I want to count on the data in same columns and restart the count if there have new data in upper level (say previous column), see my picture.
I use the formula of counta, but the numbering is incorrect (say row 8 in example)
=COUNTA(D$3:D8)&COUNTA(E$3:E8)&COUNTA(F$3:F8)&COUNTA(G$3:G8)&COUNTA(H$3:H8)&COUNTA(I$3:I8)
Is it possible to solve by vba?

I assume no Excel version constraints as per the tags in the question. You can try the following formula in
B2. The number of levels is variable so you can have more than6.Here is the output:
It is an array formula, so it spills the entire result. The name
x, generates1's from the grid:C2:H10if the value is not empty, otherwise0's. It will be our main input for the calculation. The main idea is to build a cumulative calculation with "memory", i.e. we need to take into consideration the information from the previous column.In order to build this cumulative calculation with "memory" we use a user
LAMBDAfunctionCNTS, to calculate the counts for a given column (i). TheCNTStakes into account the previous column (i-1) to reset the accumulator (ac). We useSCANto do the cumulative sum and reset the accumulator (ac) if the row of the previous corresponding column is positive, by doing this way we can start the counter again.The rest is just to do a recursive concatenation via
REDUCEfor all the levels.Notes:
SCANcall, but it can be generalized to consider all cases within the sameCNTSfunction. Therefore we have anIFcondition for the case the column is1, so we create on the fly an artificial previous column viaN(seq <0)which is a constant column with zero values.levelsmust be greater than1, which is a reasonable assumption. If you want to considerlevels=1for the sake of a more generic formula, then you can add anIFcondition beforeREDUCE, because we don't need a recursion for that, for example,IF(level=1, CNTS(1), REDUCE(...)).