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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Code:
'=cCPGup(C9:I9,"1 x 2")
Function cCPGup(sRange As Range, G As String) As Integer
  Dim f As Range, r As Range, c As Range, i As Integer, j As Integer
  Dim a, e, n As Integer
  a = Split(G, " ")
  n = UBound(a)
  For Each c In sRange
    If c.Row - n < 1 Then GoTo NextC
    Set r = Range(c.Offset(-n), c)
    j = 0
    For Each e In a
      Set f = r.Find(e)
      If Not f Is Nothing Then j = j + 1
    Next e
    If j = 3 Then i = i + 1
NextC:
  Next c
  cCPGup = i
End Function
 
Upvote 0
Code:
'=cCPGup(C9:I9,"1 x 2")
Function cCPGup(sRange As Range, G As String) As Integer
  Dim f As Range, r As Range, c As Range, i As Integer, j As Integer
  Dim a, e, n As Integer
  a = Split(G, " ")
  n = UBound(a)
  For Each c In sRange
    If c.Row - n < 1 Then GoTo NextC
    Set r = Range(c.Offset(-n), c)
    j = 0
    For Each e In a
      Set f = r.Find(e)
      If Not f Is Nothing Then j = j + 1
    Next e
    If j = 3 Then i = i + 1
NextC:
  Next c
  cCPGup = i
End Function
Hi,

Kenneth Hobson, I just place the code in module and formula in K9 and copied down but I am getting results only 0's

I would like to have macro instead function is it possible?

Thank you

Regards,
Kishan


 
Last edited:
Upvote 0
Works fine for me. Most don't know that I test 95% of what I post. Of course opinions vary for what "works".

Sure, any UDF can be ran as a Sub even more reliably. e.g.
Code:
Sub Test_cCPGup()
  MsgBox cCPGup(Range("C9:I9"), "1 X 2")
  MsgBox cCPGup(Range("C12:I12"), "1 X 2")
End Sub
If you want to test in my file, let me know.
 
Upvote 0
Works fine for me. Most don't know that I test 95% of what I post. Of course opinions vary for what "works".

Sure, any UDF can be ran as a Sub even more reliably. e.g.
Code:
Sub Test_cCPGup()
  MsgBox cCPGup(Range("C9:I9"), "1 X 2")
  MsgBox cCPGup(Range("C12:I12"), "1 X 2")
End Sub
If you want to test in my file, let me know.
Hi,

Kenneth Hobson, if I run this sub I am getting answer 1 & 1 which is correct I don't know what I am doing wrong with function here are the results


Book1
ABCDEFGHIJKL
1
2
3
4Cycle
5C1C2C3C4C5C6C7Completad
6X111212
7XX21XX1
8X1112120
9211X21X0
101X1X1120
11X2XX2X10
12X1121110
131XX21X10
141112XXX
151XX1111
16X1X212X
1711X121X
1811111X1
Sheet2
Cell Formulas
RangeFormula
K9=cCPGup(C9:I9,"1 x 2")
K10=cCPGup(C10:I10,"1 x 2")
K11=cCPGup(C11:I11,"1 x 2")
K12=cCPGup(C12:I12,"1 x 2")
K13=cCPGup(C13:I13,"1 x 2")


Thank you

Regards,
Kishan
 
Upvote 0
The only way I can duplicate that is if Calculation is set to Manual.
Hi,

Kenneth Hobson, I check in options the Calculation is set automatic, or is it because I am using version 2000? That I have mentioned in opening post

Thank you

Regards,
Kishan

 
Upvote 0
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
 
Upvote 0
Last edited:
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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