Count consecutive 0’s and 1’s

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

I have submitted example sheet below in the 20 columns C:V, I have table with 0’s and 1’s…I need a formula or VBA which can count consecutive 0’s and 1’s as shown in the columns Z:AS… if the count are correct column “AT” will sum 20...Please help.

Consecutive Question.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASAT
1
2
3
4z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSum
5z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2001010101010101010101Sum
6100010101100101100101311112211221120
71110110010110010110031221122112220
8100010101100101100101311112211221120
910011010011000011000122112242320
10010100100111000111001111212333220
1111111100100001100001621424120
12100100010010100010101213121113111120
1301110100010010010010131131212121120
14000111101101111101113412151320
15011000001001101001101251221122120
161011011110001110001111214333220
170010101000101000101021111131113111120
1800000100011100011100513333220
19100110000100110100111224122112220
Sheet1
Cell Formulas
RangeFormula
AT6:AT19AT6=SUM(Z6:AS6)


Regards,
Moti
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Due to posting size limitations, I'll show one idea in two posts. First construct some helper cells (see blue cells):
Cell Formulas
RangeFormula
AV6:BO19AV6=IF(AND(ISNUMBER(B6),C6=B6),"",COUNTA(C6:INDEX(C6:$V6,,IFERROR(MATCH(--NOT(C6=1),C6:$V6,0)-1,COLUMNS(C6:$V6)))))
 
Upvote 1
Then aligned with the helper cells, your original data set and the formula that draws from the helper cells:
MrExcel_20240301.xlsx
CDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASAT
4z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSum
5z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2001010101010101010101Sum
610001010110010110010 13111122112211     20
711101100101100101100 312211221122       20
810001010110010110010 13111122112211     20
910011010011000011000 1221122423         20
100101001001110001110011112123332         20
1111111100100001100001 6214241            20
1210010001001010001010 12131211131111     20
13011101000100100100101311312121211       20
140001111011011111011134121513            20
150110000010011010011012512211221         20
1610110111100011100011 112143332          20
1700101010001010001010211111311131111     20
18000001000111000111005133332             20
1910011000010011010011 12241221122        20
Sheet5
Cell Formulas
RangeFormula
Z6:AS19Z6=IF(AND(COLUMNS($Z:Z)=1,$C6=1),"",IFERROR(INDEX($AV6:$BO6,,AGGREGATE(15,6,COLUMN($A:$T)/($AV6:$BO6<>""),COLUMNS($Z:Z)-($C6=1))),""))
AT6:AT19AT6=SUM(Z6:AS6)
 
Upvote 1
If column W does not contain 0s or 1s and column Y does not contain numbers (both of which look like the case with your sample) and you want to try without helper cells ..

I can also only show part of the results due to XL2BB limitations.
Columns C:W exactly as you have shown in post 1.

24 03 02.xlsm
YZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASAT
4CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSum
501010101010101010101Sum
6 13111122112211     20
7 312211221122       20
8 13111122112211     20
9 1221122423         20
1011112123332         20
11 6214241            20
12 12131211131111     20
131311312121211       20
1434121513            20
1512512211221         20
Count 0 1
Cell Formulas
RangeFormula
Z6:AS15Z6=IF(OR($C6*COLUMNS($Z:Z)=1,SUM($Y6:Y6)=COLUMNS($C6:$V6)),"",IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$W6<>$C6:$V6),COLUMNS($Z:Z)-$C6),COLUMN($V6))-IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$V6<>$C6:$U6),COLUMNS($Z:Z)-$C6-1),COLUMN($C6)-1))
AT6:AT15AT6=SUM(Z6:AS6)
 
Upvote 1
Then aligned with the helper cells, your original data set and the formula that draws from the helper cells:
KRice, I am amazed to see how you make formula congratulations, really it worked fine I can adapt the situation as per your layout also no problem. I am happy and appreciate your help. 🍻

I tried using different combination instead 0’s & 1’s…1’s & 2’s it looks, I need to change the formula to get correct results, I tried to make changes using many ways but has not been possible for me to get correct answer.

Please can you help to modify as per following scenario. Also may I ask you is it possible via VBA.

Consecutive Question.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASAT
1
2
3
4z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSum
5z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2012121212121212121212Sum
6211121212211212211211311112211221120
72221221121221121221131221122112220
8211121212211212211211311112211221120
921122121122111122111122112242320
10121211211222111222111111212333220
1122222211211112211112621424120
12211211121121211121211213121113111120
1312221211121121121121131131212121120
14111222212212222212223412151320
15122111112112212112211251221122120
162122122221112221112211214333220
171121212111212111212121111131113111120
1811111211122211122211513333220
19211221111211221211221224122112220
Sheet5
Cell Formulas
RangeFormula
AT6:AT19AT6=SUM(Z6:AS6)


Have a good time and good luck.

Kind regards,
Moti :)
 
Upvote 0
If column W does not contain 0s or 1s and column Y does not contain numbers (both of which look like the case with your sample) and you want to try without helper cells ..

I can also only show part of the results due to XL2BB limitations.
Columns C:W exactly as you have shown in post 1.
Peter_SSs, I am amazed and happy to get solution exactly as per my layout. I do appreciate your help. (y)

I tried using different combination as per Post#5 instead 0’s & 1’s…1’s & 2’s it looks, I need to change the formula to get correct results, I tried to make changes using many ways but has not been possible for me to get correct answer.

Please can you help to modify as per following scenario. Also may I ask you are it possible via VBA?

Have a good time and good luck.

Kind regards,
Moti :)
 
Upvote 0
I tried using different combination as per Post#5 instead 0’s & 1’s…1’s & 2’s it looks, I need to change the formula to get correct results,
Yes, the formula was written to answer the question that you asked. ;)
I have table with 0’s and 1’s…I need a formula or VBA which can count consecutive 0’s and 1’s


Please can you help to modify as per following scenario.
I don't know. It depends just what the scenario is. Some more (small but representative) sample data & expected results with new explanation?
 
Upvote 0
Yes, the formula was written to answer the question that you asked. ;)
Peter_SSs, yes it is true and worked fine. (y)
I don't know. It depends just what the scenario is. Some more (small but representative) sample data & expected results with new explanation?
Peter_SSs, thank you, sure data 1’s & 2’s in columns C:V, and expected count result are shown in the columns X:AQ, & sum in the Column AR. Hope this help.

Book1.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQAR
1
2
3
4S.Nz1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSum
5S.Nz1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2012121212121212121212Sum
61211121212211212211211311112211221120
722221221121221121221131221122112220
83211121212211212211211311112211221120
9421122121122111122111122112242320
105121211211222111222111111212333220
11622222211211112211112621424120
127211211121121211121211213121113111120
13812221211121121121121131131212121120
149111222212212222212223412151320
1510122111112112212112211251221122120
16112122122221112221112211214333220
17121121212111212111212121111131113111120
181311111211122211122211513333220
1914211221111211221211221224122112220
201511112122122122122122
211611122112121121121121
221721211211121112121112
231811111211212221212221
241921121121121112121112
252011112111111111111111
Sheet1
Cell Formulas
RangeFormula
AR6:AR19AR6=SUM(X6:AQ6)


Kind regards,
Moti
 
Upvote 0
Put this formula in X6 and copy across and down.

Excel Formula:
=IF(OR(AND($C6<>$X$5,COLUMNS($X:X)=1),SUM($W6:W6)=COLUMNS($C6:$V6)),"",IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$W6<>$C6:$V6),COLUMNS($X:X)-($C6<>$X$5)),COLUMN($V6))-IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$V6<>$C6:$U6),COLUMNS($X:X)-($C6<>$X$5)-1),COLUMN($C6)-1))

I think that should work with any pair of numbers you use in the left section. For example

24 03 02.xlsm
CDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQAR
4z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSum
5z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2047474747474747474747Sum
674447474774474774474 13111122112211     20
777747744747744747744 312211221122       20
874447474774474774474 13111122112211     20
974477474477444477444 1221122423         20
104747447447774447774411112123332         20
Count Sequences
Cell Formulas
RangeFormula
X6:AQ10X6=IF(OR(AND($C6<>$X$5,COLUMNS($X:X)=1),SUM($W6:W6)=COLUMNS($C6:$V6)),"",IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$W6<>$C6:$V6),COLUMNS($X:X)-($C6<>$X$5)),COLUMN($V6))-IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$V6<>$C6:$U6),COLUMNS($X:X)-($C6<>$X$5)-1),COLUMN($C6)-1))
AR6:AR10AR6=SUM(X6:AQ6)
 
Upvote 1
Solution
Put this formula in X6 and copy across and down.

Excel Formula:
=IF(OR(AND($C6<>$X$5,COLUMNS($X:X)=1),SUM($W6:W6)=COLUMNS($C6:$V6)),"",IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$W6<>$C6:$V6),COLUMNS($X:X)-($C6<>$X$5)),COLUMN($V6))-IFERROR(AGGREGATE(15,6,COLUMN($C6:$V6)/($D6:$V6<>$C6:$U6),COLUMNS($X:X)-($C6<>$X$5)-1),COLUMN($C6)-1))

I think that should work with any pair of numbers you use in the left section. For example
Peter_SSs, this is the supreme solution. I tried changing variety of pair combinations truly this worked with all outstanding.

Thank you so much for optimum answer. 👌

Good night, nice weekend and good luck.

Kind regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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