Count if 3 character "1 X 2" are find in the 3 rows.

Kishan

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

Hi,


Count if 3 character "1 X 2" are find in the 3 rows no matter the if they are not in sequence (I mean can be "1X2", "X12", "2X1" or in any sequence no problem)

Data are in cells C6:I80 (count results in K6:K80

Look "1X2" into 3 rows STARTING FROM C6 for example....

Step1-Start looking in C6, C7, and C8 - D6, D7, and D8 - E6, E7, and E8 - F6, F7, and F8 - G6, G7, and G8 - H6, H7, and H8 - I6, I7, and I8 (DO NOT FOUND "1X2" RESULT = IN K8=0)

Step2-Start looking IN NEXT 3 ROWS C7, C8, and C9 - D7, D8, and D9 - E7, E8, and E9 - F7, F8, and F9 - G7, G8, and G9 - H7, H8, and H9 - I7, I8, and I8 (FOUND "1X2" in - I7, I8, and I8 RESULT = IN K9=1

Step3-Start looking IN NEXT 3 ROWS C8, C9, and C10 - D8, D9, and D10 - E8, E9, and E10 - F8, F9, and F10 - G8, G9, and G10 - H8, H9, and H10 - I8, I9, and I10 (FOUND "1X2" in - C8, C9, and C10 RESULT = IN K10=1

And continue finding into next 3 rows till end and result in column K

Example....


Book1
ABCDEFGHIJKL
1
2
3
4Cycle
5C1C2C3C4C5C6C7Completad
6X111212
7XX21XX1
8X1112120
9211X21X1
101X1X1121
11X2XX2X13
12X1121111
131XX21X11
141112XXX
151XX1111
16X1X212X
1711X121X
1811111X1
19X11XX11
20111121X
211112211
2221X11X1
231111121
24X112112
251111X11
26X111121
2712XXXX1
28111212X
29111221X
30111X111
31111X111
321X11211
332211X1X
341X12111
3521112X1
361XX2112
37X111111
38X211XX1
39X12121X
40111XX12
41XXX11X1
422X21X1X
43XX12X12
44XX1X111
4511X1121
461111X22
472X11111
48121X212
49X1X1212
50X1111X1
51X1X11X2
52X111XX1
53X1122X1
5421X21XX
55XX21X11
561X2X121
57X2X1111
5811X11X1
5911X111X
6021X1112
61X122X11
622111221
631112111
641X21X11
651X1X11X
66XXX1111
67XX22121
681121111
69X111X11
701112X1X
711X1112X
7211X1XXX
731X1X1X2
7411X2211
751XX1X11
761XXXX12
772X1X111
7811X1222
791111X11
80111121X
81
82
Sheet2


Thank you in advance

Regards,
Kishan
 
I got Kenneth's UDF to work fine. But in case you still have issues getting it to work, here's a basic subroutine you can try:

Code:
Sub CheckCycle()
Dim r As Long, c As Long, ctr As Long, x As Boolean

    For r = 9 To Cells(Rows.Count, "C").End(xlUp).Row
        ctr = 0
        For c = 3 To 9
            x = (WorksheetFunction.CountIf(Cells(r - 2, c).Resize(3), 1) > 0) And _
                (WorksheetFunction.CountIf(Cells(r - 2, c).Resize(3), 2) > 0) And _
                (WorksheetFunction.CountIf(Cells(r - 2, c).Resize(3), "X") > 0)
            If x Then ctr = ctr + 1
        Next c
        Cells(r, "K") = ctr
    Next r
    
End Sub
Thank you Eric, spot on!! It is resulting as request 100% Ok.

I appreciate very much your help and time.

Good Luck

Regards,
Kishan

 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi, well it is solved; as per opening post I need if possible could be modified existing code or new one that can check "1X2" cycle within 4 rows rather than 3

Look "1X2" pattern into 4 rows STARTING FROM C6 for example....

Step1-Start looking in C6, C7, C8 and C9 - D6, D7, D8 and D9 - E6, E7, E8 and E9 - F6, F7, F8 and F9 - G6, G7, G8 and G9 - H6, H7, H8 and H9 - I6, I7, I8 and I9 (FOUND "221X" in - I6, I7, I8 and I9 RESULT IN K9=1

And continue finding into next 4 rows till end and result in column K

PATTERN COULD BE ANY "212X", "XX21", "112X", "11X2", 2X21"
IF START WITH "1" 4TH CHARECTER SHOULD BE END WITH "X" or "2"
IF START WITH "X" 4TH CHARECTER SHOULD BE END WITH "1" or "2"
IF START WITH "2" 4TH CHARECTER SHOULD BE END WITH "1" or "X"

Example....


Book1
ABCDEFGHIJKL
1
2
3
4Cycle
5C1C2C3C4C5C6C7Completad
6X111212
7XX21XX1
8X111212
9211X21X1
101X1X1122
11X2XX2X12
12X112111
131XX21X1
141112XXX
151XX1111
16X1X212X
1711X121X
1811111X1
19X11XX11
20111121X
211112211
2221X11X1
231111121
24X112112
251111X11
26X111121
2712XXXX1
28111212X
29111221X
30111X111
31111X111
321X11211
332211X1X
341X12111
3521112X1
361XX2112
37X111111
38X211XX1
39X12121X
40111XX12
41XXX11X1
422X21X1X
43XX12X12
44XX1X111
4511X1121
461111X22
472X11111
48121X212
49X1X1212
50X1111X1
51X1X11X2
52X111XX1
53X1122X1
5421X21XX
55XX21X11
561X2X121
57X2X1111
5811X11X1
5911X111X
6021X1112
61X122X11
622111221
631112111
641X21X11
651X1X11X
66XXX1111
67XX22121
681121111
69X111X11
701112X1X
711X1112X
7211X1XXX
731X1X1X2
7411X2211
751XX1X11
761XXXX12
772X1X111
7811X1222
791111X11
80111121X
81
82
Sheet3


Thank you in advance

Regards,
Kishan
 
Last edited:
Upvote 0
You just need to change the long comparison line:

Rich (BB code):
Sub CheckCycle()
Dim r As Long, c As Long, ctr As Long, x As Boolean

    For r = 9 To Cells(Rows.Count, "C").End(xlUp).Row
        ctr = 0
        For c = 3 To 9
            x = (WorksheetFunction.CountIf(Cells(r - 3, c).Resize(4), 1) > 0) And _
                (WorksheetFunction.CountIf(Cells(r - 3, c).Resize(4), 2) > 0) And _
                (WorksheetFunction.CountIf(Cells(r - 3, c).Resize(4), "X") > 0) And _
                (Cells(r - 3, c) <> Cells(r, c))
            If x Then ctr = ctr + 1
        Next c
        Cells(r, "K") = ctr
    Next r
    
End Sub
 
Upvote 0
You just need to change the long comparison line:

Rich (BB code):
Sub CheckCycle()
Dim r As Long, c As Long, ctr As Long, x As Boolean

    For r = 9 To Cells(Rows.Count, "C").End(xlUp).Row
        ctr = 0
        For c = 3 To 9
            x = (WorksheetFunction.CountIf(Cells(r - 3, c).Resize(4), 1) > 0) And _
                (WorksheetFunction.CountIf(Cells(r - 3, c).Resize(4), 2) > 0) And _
                (WorksheetFunction.CountIf(Cells(r - 3, c).Resize(4), "X") > 0) And _
                (Cells(r - 3, c) <> Cells(r, c))
            If x Then ctr = ctr + 1
        Next c
        Cells(r, "K") = ctr
    Next r
    
End Sub
Thank you so much Eric, for your kind help it is working perfect as per new request.

Have a nice day

Good Luck

Regards,
Kishan :)
 
Upvote 0
Hi Eric,

I think modified code is looking 3 characters "1X2" into 4 rows but not the cycle

Cycle of 3 characters "1X2" for example...
If cycle start with "1" has to finish with "X or 2"
If cycle start with "X" has to finish with "1 or 2"
If cycle start with "2" has to finish with "1 or X"

I have calculated manually and shown expected results in column K and in the column L results are generated by the macro

Results require as per column K


Book1
ABCDEFGHIJKLM
1
2
3
4Cycle 4 RowsCycle 4 Rows
5C1C2C3C4C5C6C7Result OkBy Macro
6X111212
7XX21XX1
8X111212
9211X21X11
101X1X11223
11X2XX2X112
12X11211102
131XX21X100
141112XXX12
151XX111100
16X1X212X11
1711X121X12
1811111X111
19X11XX1123
20
Sheet3


Please could you check?

Thank you in advance

Regards,
Kishan
 
Upvote 0
I don't see the problem. In K10, you say you expect 2, when the macro returns 3. C7:C10 = X,X,2,1. G7:G10 = X,2,2,1. I7:I10 = 1,2,X,2. Each of those sets contains 1,2, and X, and each of them ends with a different character than it starts with. Which one of them should not be counted, and why?
 
Upvote 0
I don't see the problem. In K10, you say you expect 2, when the macro returns 3. C7:C10 = X,X,2,1. G7:G10 = X,2,2,1. I7:I10 = 1,2,X,2. Each of those sets contains 1,2, and X, and each of them ends with a different character than it starts with. Which one of them should not be counted, and why?
Hi Eric, thank you for your reply

This is ok C7:C10 = X, X, 2,1. Cycle is perfect started with X and end with 1
This is ok G7:G10 = X, 2, 2,1. Cycle is perfect started with X and end with 1

This is not ok I7:I10 = 1, 2, X, 2. If you look this pattern first three characters 1, 2, X has completed the cycle and 2 is creating a new cycle

I mean repeated characters could be in-between 1st and 4th characters
So far I7:I10 = 1, 2, X, 2. If this would have been 1, 2, 2, X were ok

More examples ´
X, 2, 1, 2 not ok, but X, 2, 2, 1 is ok
2, 1, X, 2 not ok, but 2, 1, 2, X is ok

Hope this help

Regards,
Kishan
 
Upvote 0
Say you have this sequence: 1, 2, X, 2, 1 starting on row 6. Should the 1,2,X be included in the total on row 8? Should the 1,2,X,2 be counted on row 9, because there is not a total on row 8? Should the X,2,1 be included in the total on row 10, since the X has been included in either row 8 or 9?

If I have 1,2,X,1,2,X,1,2,X,1,2,X,1,2 I won't count it on row 8 even though it completes the cycle. I won't count it on row 9 because it starts and ends with 1. I won't count it on row 10 because it starts and ends with 2. I won't count it on 11 because it starts and ends with X. In short, it won't count any of the completed cycles?
 
Upvote 0
Say you have this sequence: 1, 2, X, 2, 1 starting on row 6. Should the 1,2,X be included in the total on row 8? Should the 1,2,X,2 be counted on row 9, because there is not a total on row 8? Should the X,2,1 be included in the total on row 10, since the X has been included in either row 8 or 9?

If I have 1,2,X,1,2,X,1,2,X,1,2,X,1,2 I won't count it on row 8 even though it completes the cycle. I won't count it on row 9 because it starts and ends with 1. I won't count it on row 10 because it starts and ends with 2. I won't count it on 11 because it starts and ends with X. In short, it won't count any of the completed cycles?
Hi Eric, thank you for your reply

Yes I understand your viewpoint you are correct the way you are thinking.

1st of all I want to look cycle in the 4 rows

Found 1 cycle in the row 6, 7, 8, 9 only in the column I, started with 2 followed by 1 (not closed by X) than followed by 2 again and finally looked by missing X




Book1
ABCDEFGHIJKL
1
2
3
4Cycle 4 RowsCycle 4 Rows
5C1C2C3C4C5C6C7Result OkBy Macro
6X111212
7XX21XX1
8X111212
9211X21X11
Sheet3


Found 2 cycle in the row 7, 8, 9, 10 in the column C, started with X than followed by X again than followed by 2 again and finally looked by missing 1 & in G started with X
followed by 2 (not closed by 1) than followed by 2 again and finally looked by missing


Book1
ABCDEFGHIJKL
1
2
3
4Cycle 4 RowsCycle 4 Rows
5C1C2C3C4C5C6C7Result OkBy Macro
6
7XX21XX1
8X111212
9211X21X
101X1X11223
Sheet3


Found 1 cycle in the row 8, 9, 10, 11 in the column I


Book1
ABCDEFGHIJKL
1
2
3
4Cycle 4 RowsCycle 4 Rows
5C1C2C3C4C5C6C7Result OkBy Macro
6
7
8X111212
9211X21X
101X1X112
11X2XX2X112
Sheet3


Not found any cycle in the row 9, 10, 11, 11 in any column


Book1
ABCDEFGHIJKL
1
2
3
4Cycle 4 RowsCycle 4 Rows
5C1C2C3C4C5C6C7Result OkBy Macro
6
7
8
9211X21X
101X1X112
11X2XX2X1
12X11211102
Sheet3


So these ways keep tracking into 4 next rows

Regards,
Kishan
 
Upvote 0
Hi Eric, I tried to make all possible pattern could be made which complete the cycle. I found total 18, if find any out of these 18 within 4 rows can be counted for the total

Here are the pattern cycle lists....


Book1
ABCDEFGHIJKLMNOPQRST
1
2
3
4123456789101112131415161718
5
6111111XXXXXX222222
711XX2211XX2211XX22
8X21X121X12X212X21X
92X22XX222111XX11X1
10
11
Sheet1


Hope this help

Regards,
Kishan
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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