Count cycle 1&0 or 0&1 visa versa.

motilulla

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

In the 2 columns C & D there are filled numbers 0’s & 1’s, I want column C count result in column F and column D count result in column G.

For example 0&1 or 1&0 is one cycle…1st cycle start in C6 with 0 and end with 1 in C10. Cycle count result in F6:F10 =1, 2, 3, 4, 5….2nd cycle start in C11 with 0 and end with 1 in C13. Cycle count result in F11:F13 =1, 2, 3….and so on…

Same logic will be applied for column D and results in column G.
Note: columns are coloured just to explain example clearer.

Please advise any formula or VBA solution.

MrExcel Question.xlsm
ABCDEFGH
1
2
3
4
5n1n2n1n2
60111
70122
80133
90144
101155
110116
120027
131131
141112
151123
160034
171111
181122
190133
201014
211021
220132
231111
240122
251113
260124
271015
281021
291132
301041
311152
321061
330072
341113
350021
361012
370123
380111
390022
401031
410112
420121
430132
440143
451154
461115
470026
481111
490122
500113
510024
520131
530142
540053
550061
561072
570113
581124
591115
600126
611017
621021
631032
64
Count Cycle


Regards,
Moti
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try this UDF. Paste this into a VBA module.
VBA Code:
Function ZeroOneCycle(rng As Range) As Variant
    Dim a As Range, b()
    Dim lastRow As Long
    Dim i As Long, k As Long
    Dim startCycle As Long
  
    lastRow = rng.Rows.Count
    startCycle = rng.Cells(1, 1).Value
    ReDim b(1 To lastRow, 1 To 1)
  
    k = 1
    b(1, 1) = k
  
    For i = 2 To lastRow
        If rng.Cells(i, 1).Value = startCycle Then
            k = k + 1
            b(i, 1) = k
        Else
            b(i, 1) = k + 1
            If i <= lastRow Then startCycle = rng.Cells(i + 1, 1).Value
            k = 0
        End If
    Next i

    CountCycles = b
End Function

In the output cell, enter
Excel Formula:
=ZeroOneCycle(C3:C63)
 
Upvote 1
VBA Code:
Function ZeroOneCycle(rng As Range) As Variant
    Dim a As Range, b()
    Dim lastRow As Long
    Dim i As Long
    Dim startCycle As Long
    
    lastRow = rng.Rows.Count
    startCycle = rng.Cells(1, 1).Value
    ReDim b(1 To lastRow, 1 To 1)
    
    k = 1
    b(1, 1) = k
    
    For i = 2 To lastRow
        If rng.Cells(i, 1).Value = startCycle Then
            k = k + 1
            b(i, 1) = k
        Else
            b(i, 1) = k + 1
            If i <= lastRow Then startCycle = rng.Cells(i + 1, 1).Value
            k = 0
        End If
    Next i

    ZeroOneCycle = b
End Function
 
Upvote 1
Solution
Cubist, “Function ZeroOneCycle” worked perfect as per my request

I admire your help and thank you for the time you spent. Good Luck!

Kind Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,609
Members
452,660
Latest member
Zatman

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