Count "Start & End patterns" for each cycle done.

Kishan

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

Hi,

Perhaps this query is not so easy as title looks in fact the question is 2 in 1, 1st find the cycle and then the count's

Data are in cells C6:C82 change colour shows the cycle for "1X2" - E6:F11 are the start & end cycle patterns - G6:G11 is the result counts of pattern cycle

For example...
In the each cycle there would be 6 patterns 1-X, 1-2, X-1, X-2, 2-1 & 2-X as specified in cells E6:F11

1st cycle of "1X2" start with "X" in C6 and end with "1" in cell C10 (so X-1 counts will go in cells G8)

2nd cycle of "1X2" start with "X" in C11 and end with "2" in cell C20 (so X-2 counts will go in cells G9)

And so on...

Example data...


Book1
ABCDEFGH
1
2
3
4RESULT
5C1START WITHEND WITHCOUNTS
6X1X3
7X121
8XX12
92X23
101211
11X2X1
12X
131
141
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
55X
562
571
582
592
60X
612
622
63X
641
651
661
672
68X
692
701
712
722
731
741
751
762
772
782
791
80X
811
82X
83
84
85
86
Sheet1


Thank you in advance

Kishan
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try:

Code:
Sub GetCounts()
Dim counts(1 To 3) As Long, r As Long, i As Long, StartVal As String, res(1 To 6, 1 To 1), se As Variant

    r = 6
    se = Array("1X", "12", "X1", "X2", "21", "2X")
    StartVal = Cells(r, "C").Value
    While Cells(r, "C") <> ""
        If Cells(r, "C") = 1 Then counts(1) = 1
        If Cells(r, "C") = 2 Then counts(2) = 1
        If Cells(r, "C") = "X" Then counts(3) = 1

        If WorksheetFunction.Product(counts) > 0 Then
            StartVal = StartVal & Cells(r, "C").Value
            i = WorksheetFunction.Match(StartVal, se, 0)
            res(i, 1) = res(i, 1) + 1
            Erase counts
            StartVal = Cells(r + 1, "C").Value
        End If
        r = r + 1
    Wend
    Range("G6:G11").Value = res
    
End Sub
 
Upvote 0
Try:

Code:
Sub GetCounts()
Dim counts(1 To 3) As Long, r As Long, i As Long, StartVal As String, res(1 To 6, 1 To 1), se As Variant

    r = 6
    se = Array("1X", "12", "X1", "X2", "21", "2X")
    StartVal = Cells(r, "C").Value
    While Cells(r, "C") <> ""
        If Cells(r, "C") = 1 Then counts(1) = 1
        If Cells(r, "C") = 2 Then counts(2) = 1
        If Cells(r, "C") = "X" Then counts(3) = 1

        If WorksheetFunction.Product(counts) > 0 Then
            StartVal = StartVal & Cells(r, "C").Value
            i = WorksheetFunction.Match(StartVal, se, 0)
            res(i, 1) = res(i, 1) + 1
            Erase counts
            StartVal = Cells(r + 1, "C").Value
        End If
        r = r + 1
    Wend
    Range("G6:G11").Value = res
    
End Sub
Awesome! Eric, Exactly what I was looking for :cool:

Thank you so much for your precious time and help.

Kind Regards,
Kishan :)


 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,600
Members
452,658
Latest member
GStorm

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