Count the first non-zero Consecutive Cells until another 0 appears

tuqiuchiaa

New Member
Joined
Mar 12, 2018
Messages
3
Hi,
I'm trying to figure out a way to count the number of cells in the first non-zero Consecutive cells.
Been reading all the posts about consecutive cells posts, but couldn't get anything working.
Could anyone help? Thanks in advance!
The desired results are listed below

ABCDEFResult
2120003
002032002
00110012
102312211

<tbody>
</tbody>
 
A possible formula solution

A
B
C
D
E
F
G
H
1
Val1​
Val2​
Val3​
Val4​
Val5​
Val6​
Result​
2
2​
1​
2​
0​
0​
0​
3​
3
0​
0​
20​
32​
0​
0​
2​
4
0​
0​
1​
10​
0​
1​
2​
5
1​
0​
231​
2​
1​
1​
1​
6
1​
0​
4​
6​
0​
0​
1​
7
0​
0​
0​
0​
0​
0​
0​

<tbody>
</tbody>


Array formula in H2 copied down
=IFERROR(INDEX(FREQUENCY(IF(A2:F2<>0,COLUMN(A2:F2)),IF(A2:F2=0,COLUMN(A2:F2))),MATCH(1,--(FREQUENCY(IF(A2:F2<>0,COLUMN(A2:F2)),IF(A2:F2=0,COLUMN(A2:F2)))>0),0)),0)
Ctrl+Shift+Enter

M.
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I think you need to add some error handling code in there. That function blows up if all the cells are 0 (it returns the #VALUE ! error).
Good catch... I forgot about that being a real possibility. The fix is easy... we just need to make sure the Split function has something to split on, so I simply concatenate a blank onto the end of the result from the Trim operation...
Code:
[table="width: 500"]
[tr]
	[td]Function Consec(myRange As Range) As Long
  Consec = Len(Split(Application.Trim(Join(Evaluate("IF(" & myRange.Address & "=0,"" "",""X"")"), "")) [B][COLOR="#FF0000"]& " "[/COLOR][/B])(0))
End Function[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Aladin,

I came across an issue with this solution.
It works fine as long as there are BOTH zeroes and numbers.
But, if all six cells are 0, it will return a 6 (instead of 0).
And if all six cells are numbers other than 0, it will return 0 (instead of 6).

I assumed the usual, hastily :ROFLMAO: as it appears... Marcelo has a formula proposal for the case, though.
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,778
Members
449,336
Latest member
p17tootie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top