Count cycle period

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Hi,

I need to count cycle of 1X2 period when each time it is completed.

Data in Cells C6:C66 Result in cells E6:E14 colours are just shown to show example clearly.

Example data


Book1
ABCDEF
1
2
3
4
5C1Count Cycle
6X5
7X12
8X13
927
1015
11X7
12X3
1314
1415
151
16X
171
181
19X
201
211
222
231
24X
251
26X
271
281
291
301
311
321
332
341
352
361
37X
38X
39X
401
41X
422
43X
44X
451
461
472
481
49X
50X
51X
52X
53X
542
551
56X
572
581
591
602
61X
622
631
641
651
66X
67
68
Sheet1


Thank you in advance

Kishan
 

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.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Sep39
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, C [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Fd1 [COLOR="Navy"]As[/COLOR] Boolean, Fd2 [COLOR="Navy"]As[/COLOR] Boolean, Fd3 [COLOR="Navy"]As[/COLOR] Boolean
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("C6"), Range("C" & Rows.Count).End(xlUp))
Rw = 5
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    n = n + 1
    [COLOR="Navy"]If[/COLOR] Not Fd1 And Dn.Value = "X" [COLOR="Navy"]Then[/COLOR] Fd1 = True: C = C + 1
    [COLOR="Navy"]If[/COLOR] Not Fd2 And Dn.Value = 1 [COLOR="Navy"]Then[/COLOR] Fd2 = True: C = C + 1
    [COLOR="Navy"]If[/COLOR] Not Fd3 And Dn.Value = 2 [COLOR="Navy"]Then[/COLOR] Fd3 = True: C = C + 1
    [COLOR="Navy"]If[/COLOR] C = 3 [COLOR="Navy"]Then[/COLOR]
        Fd1 = False: Fd2 = False: Fd3 = False
        Rw = Rw + 1
        Cells(Rw, "E") = n
        n = 0: C = 0
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
without the colors how do we know how many rows are to be considered each time
Hi oldbrewer, I do not need to count the colour cells I have tried to show by colour in the example that when the each time Cycle of 1, X & 2 has completed.

For example...

1st time 1, X & 2 cycle is completed in cells C6:C10 and the result 5 in cell E6 so I have highlighted the cells C6:C10 in cyan and the result cell E6 in colour cyan.

2nd 1, X & 2 cycle is completed in cells C11:C22 and the result 5 in cell E7 so I have highlighted the cells C11:C22 in magenta and the result cell E7 in colour magenta.

And so on...

Hope this help

And so on...

Regards,
Kishan


 
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG12Sep39
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, C [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Rw [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] Fd1 [COLOR=navy]As[/COLOR] Boolean, Fd2 [COLOR=navy]As[/COLOR] Boolean, Fd3 [COLOR=navy]As[/COLOR] Boolean
[COLOR=navy]Set[/COLOR] Rng = Range(Range("C6"), Range("C" & Rows.Count).End(xlUp))
Rw = 5
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    n = n + 1
    [COLOR=navy]If[/COLOR] Not Fd1 And Dn.Value = "X" [COLOR=navy]Then[/COLOR] Fd1 = True: C = C + 1
    [COLOR=navy]If[/COLOR] Not Fd2 And Dn.Value = 1 [COLOR=navy]Then[/COLOR] Fd2 = True: C = C + 1
    [COLOR=navy]If[/COLOR] Not Fd3 And Dn.Value = 2 [COLOR=navy]Then[/COLOR] Fd3 = True: C = C + 1
    [COLOR=navy]If[/COLOR] C = 3 [COLOR=navy]Then[/COLOR]
        Fd1 = False: Fd2 = False: Fd3 = False
        Rw = Rw + 1
        Cells(Rw, "E") = n
        n = 0: C = 0
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Thank you MickG, your code is giving the answers correct even mine data are not correct in the opening post#1

It should be the result as your code is generating as shown below which is 100% Perfect!! :)


Book1
DEF
1
2
3
4
5Count Cycle
65
712
811
94
105
115
127
133
144
155
16
17
Sheet1


Thank you so much for your help

Kind Regards,
Kishan :)
 
Upvote 0
Just as an interesting side note, you can do this with formulas too:

CDE
5C1Count Cycle
6X5
7X12
8X11
924
1015
11X5
12X7
1313
1414
1515
16X
171
181
19X
201
211
222
231
24X
251
26X
271
281
291
301
311
321
332
341
352
361
37X
38X
39X
401
41X
422
43X
44X
451
461
472
481
49X
50X
51X
52X
53X
542
551
56X
572
581
591
602
61X
622
631
641
651
66X

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet11

Worksheet Formulas
CellFormula
E6=IFERROR(MAX(MATCH("X",OFFSET($C$6:$C$66,SUM(E$5:E5),0),0),MATCH(1,OFFSET($C$6:$C$66,SUM(E$5:E5),0),0),MATCH(2,OFFSET($C$6:$C$66,SUM(E$5:E5),0),0)),"")

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
Just as an interesting side note, you can do this with formulas too:
Hi Eric, the formula you suggested it is not working with my version I tried to change but no results.

Thank you for your help

Kind Regards,
Kishan
 
Upvote 0
I think you have Excel 2000? That doesn't have the IFERROR function. You can just remove it like this:

=MAX(MATCH("X",OFFSET($C$6:$C$66,SUM(E$5:E5),0),0),MATCH(1,OFFSET($C$6:$C$66,SUM(E$5:E5),0),0),MATCH(2,OFFSET($C$6:$C$66,SUM(E$5:E5),0),0))

I believe the rest of the functions work in 2000. You'll just get an error at the end of the list instead of an empty cell.
 
Upvote 0

Forum statistics

Threads
1,215,548
Messages
6,125,464
Members
449,229
Latest member
doherty22

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