Count consecutive 0’s and 1’s

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
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
 
Just getting back to looking at this. To make my offering work for 1's/2's....you need something in the helper formula to switch back and forth between those two values, so I've swapped out
--NOT(C6=1) for this instead...(C6=1)+1
So the helper formula becomes:
Excel Formula:
=IF(AND(ISNUMBER(B6),C6=B6),"",COUNTA(C6:INDEX(C6:$V6,,IFERROR(MATCH((C6=1)+1,C6:$V6,0)-1,COLUMNS(C6:$V6)))))
And then to position the final results correctly in the main results table, the two checks for $C6=0 become $C6=1, so the main formula is as follows:
MrExcel_20240301.xlsx
ZAAABAC
4CountCountCountCount
50101
6 131
7 312
8 131
Sheet5_01
Cell Formulas
RangeFormula
Z6:AC8Z6=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))),""))
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
KRice, thank you, following your instruction I changed formula what I am doing wrong not getting expected results. Please can you revise it?

Consecutive Question.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBP
1
2
312121212121212121212
4z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSumCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSum
5z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2001010101010101010101Sum01010101010101010101Sum
621112121221121221121119115113210172411      78119  1151132 10 172 4 1178
722212211212211212211317214111281522        683  172 14 1112 8 152 2 68
821112121221121221121119115113210172411      78119  1151132 10 172 4 1178
Sheet1
Cell Formulas
RangeFormula
Z6:AS8Z6=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))),""))
BP6:BP8,AT6:AT8AT6=SUM(Z6:AS6)
AV6:BO8AV6=IF(AND(ISNUMBER(B6),C6=B6),"",COUNTA(C6:INDEX(C6:$V6,,IFERROR(MATCH(--NOT(C6=1),C6:$V6,0)-1,COLUMNS(C6:$V6)))))


Kind regards,
Moti
 
Upvote 0
Thanks for asking...and sorry, it looks as if I made a mistake when I edited the formula to work with 1's and 2's. The final results formula should use a $C6=2 in two places, rather than $C6=1 that I mentioned previously. And in the helper table, it looks like you did not revise the formula as I described previously. One note about the helper table, I would not label its columns with 1's and 2's or 0's and 1's, as the helper table only counts the number of adjacent cells with matching values, and then as it moves along the row, it counts the number of adjacent cells with the alternating value. But the helper table results do not align correctly with a 0/1 or 1/2 column. The alignment is done in the final formula...the main results table.
MrExcel_20240301.xlsx
CDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBP
4z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSumSum
5z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2012121212121212121212Sum1234567891011121314151617181920Sum
621112121221121221121 13111122112211     2013  11112 2 112 2 1120
722212211212211212211 312211221122       203  12 2 112 2 112 2 20
821112121221121221121 13111122112211     2013  11112 2 112 2 1120
Sheet9
Cell Formulas
RangeFormula
Z6:AS8Z6=IF(AND(COLUMNS($Z:Z)=1,$C6=2),"",IFERROR(INDEX($AV6:$BO6,,AGGREGATE(15,6,COLUMN($A:$T)/($AV6:$BO6<>""),COLUMNS($Z:Z)-($C6=2))),""))
AT6:AT8,BP6:BP8AT6=SUM(Z6:AS6)
AV6:BO8AV6=IF(AND(ISNUMBER(B6),C6=B6),"",COUNTA(C6:INDEX(C6:$V6,,IFERROR(MATCH((C6=1)+1,C6:$V6,0)-1,COLUMNS(C6:$V6)))))
 
Upvote 1
Thanks for asking...and sorry, it looks as if I made a mistake when I edited the formula to work with 1's and 2's. The final results formula should use a $C6=2 in two places, rather than $C6=1 that I mentioned previously. And in the helper table, it looks like you did not revise the formula as I described previously. One note about the helper table, I would not label its columns with 1's and 2's or 0's and 1's, as the helper table only counts the number of adjacent cells with matching values, and then as it moves along the row, it counts the number of adjacent cells with the alternating value. But the helper table results do not align correctly with a 0/1 or 1/2 column. The alignment is done in the final formula...the main results table.
MrExcel_20240301.xlsx
CDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBP
4z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z20CountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountCountSumSum
5z1z2z3z4z5z6z7z8z9z10z11z12z13z14z15z16z17z18z19z2012121212121212121212Sum1234567891011121314151617181920Sum
621112121221121221121 13111122112211     2013  11112 2 112 2 1120
722212211212211212211 312211221122       203  12 2 112 2 112 2 20
821112121221121221121 13111122112211     2013  11112 2 112 2 1120
Sheet9
Cell Formulas
RangeFormula
Z6:AS8Z6=IF(AND(COLUMNS($Z:Z)=1,$C6=2),"",IFERROR(INDEX($AV6:$BO6,,AGGREGATE(15,6,COLUMN($A:$T)/($AV6:$BO6<>""),COLUMNS($Z:Z)-($C6=2))),""))
AT6:AT8,BP6:BP8AT6=SUM(Z6:AS6)
AV6:BO8AV6=IF(AND(ISNUMBER(B6),C6=B6),"",COUNTA(C6:INDEX(C6:$V6,,IFERROR(MATCH((C6=1)+1,C6:$V6,0)-1,COLUMNS(C6:$V6)))))
KRice, I appreciate your time and help. All worked perfect as I wanted, (y) thanks for about the helper column note that helps.

Have a beautiful weekend and good luck.

Kind regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,215,079
Messages
6,123,009
Members
449,093
Latest member
ikke

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