Count 1-X-2 Before "X" 11th Pos

Kishan

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

I need to count in the each row 1, X & 2 before the X is finding in the 11th position (and stop counting if breaks with other sign is found). Example is attached


Book1
ABCDEFGHIJKLMNOPQRST
1YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14EmptyBefore "X" 11th PositionBefore "X" 11th PositionBefore "X" 11th Position
2YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14EmptyCount 1Count XCount 2
3201301/12/20132X2111X1112X11
4201302/12/201322X121XX21212X
5201303/12/201311111X1X11XX122
6201304/12/201311121112X111X1
7201305/12/20131X11X12X11111X
8201306/12/201311122X111X111X
9201307/12/201311X111121X11X1
10201308/12/2013121111211X1112
11201309/12/201322X2112X211X12
12201310/12/2013111X1221121X11
13201311/12/20131211X1111XX1XX1
14201312/12/2013112X2212X11X21
15201313/12/20131X1X1222111222
16201314/12/2013XX11211222X1113
17201315/12/201322222X1112X2X11
18201316/12/2013X111XXX111X1223
19201317/12/201322X1212121X11X1
20201318/12/2013X1121121X2X2211
21201319/12/2013111212X22X1211
22201320/12/20132111X211X11122
Count 1-X-2 Before "X" 11th Pos


Need VBA solution if possible

Please help!

Thanks

Regards,
Kishan
 
Hello,

sorry, rushing too much

Code:
Sub COUNT_COLOUR_1_2_X()
    Columns("C:T").Interior.ColorIndex = xlNone
    MY_POSITION = InputBox("Plase enter X Position", "X POSITION") + 2
    For MY_ROWS = 3 To Range("A" & Rows.Count).End(xlUp).Row
        If Cells(MY_ROWS, MY_POSITION).Value = "X" Then
            Cells(MY_ROWS, MY_POSITION).Interior.ColorIndex = 6
            MY_VALUE = Cells(MY_ROWS, MY_POSITION - 1)
            For MY_COLS = MY_POSITION - 1 To 3 Step -1
                If Cells(MY_ROWS, MY_COLS).Value = MY_VALUE Then
                    Select Case MY_VALUE
                        Case 1
                            Cells(MY_ROWS, MY_COLS).Interior.ColorIndex = 3 'red
                        Case "X"
                            Cells(MY_ROWS, MY_COLS).Interior.ColorIndex = 5 'blue
                        Case 2
                            Cells(MY_ROWS, MY_COLS).Interior.ColorIndex = 7
                End Select
                    MY_COUNT = MY_COUNT + 1
                Else
                    GoTo CONT
                End If
            Next MY_COLS
CONT:
            Select Case MY_VALUE
                Case 1
                    Cells(MY_ROWS, 18).Value = MY_COUNT
                    Cells(MY_ROWS, 18).Interior.ColorIndex = 3
                Case "X"
                    Cells(MY_ROWS, 19).Value = MY_COUNT
                    Cells(MY_ROWS, 19).Interior.ColorIndex = 5
                Case 2
                    Cells(MY_ROWS, 20).Value = MY_COUNT
                    Cells(MY_ROWS, 20).Interior.ColorIndex = 7
            End Select
            MY_COUNT = 0
        End If
    Next MY_ROWS
End Sub
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hello, sorry, rushing too much
Hi onlyadrafter,

1st row 1 and 2 header format should not trun off
2nd counting 1 instance more

Result Requir

Book1
VWXY
1EmptyBefore "X" 11th PositionBefore "X" 11th PositionBefore "X" 11th Position
2EmptyCount 1Count XCount 2
3
4
52
6
7
8
9
10
11
12
131
14
15
163
171
183
191
201
21
22
Count 1-X-2 Before "X" 11th Pos


Getting these results

Book1
RST
1Before "X" 11th PositionBefore "X" 11th PositionBefore "X" 11th Position
2Count 1Count XCount 2
3
4
53
6
7
8
9
10
11
12
132
14
15
164
172
184
192
202
21
22
Count 1-X-2 Before "X" 11th Pos


Please will you take a look?

Thank you

Regards,
Kishan
 
Last edited:
Upvote 0
Here is another macro that you can consider (it asks you the positions number and handles all the coloring as well)...
Code:
Sub CountsBeforeXs()
  Dim X As Long, Pos As Long, Count As Long, Cell As Range, Item As String
  Pos = Application.InputBox("Which position number (2 through 14)?", Type:=1)
  If Pos >= 2 And Pos <= 14 Then
    For Each Cell In Range(Cells(3, 2 + Pos), Cells(Rows.Count, 2 + Pos). _
                     End(xlUp)).SpecialCells(xlConstants, xlTextValues)
      Cell.Interior.Color = vbYellow
      Item = Cell.Offset(, -1).Value
      Count = 1
      For X = 2 To Pos - 1
        If Cell.Offset(, -X) = Item Then
          Count = Count + 1
        Else
          Exit For
        End If
      Next
      With Cell.Offset(, 1 - X).Resize(, X - 1)
        .Interior.ColorIndex = InStr("  1 X 2", Item)
        .Font.Color = vbWhite
      End With
      With Intersect(Cell.EntireRow, Columns("Q").Offset(, InStr("1X2", Item)))
        .Value = Count
        .Interior.ColorIndex = Cell.Offset(, -1).Interior.ColorIndex
        .Font.Color = vbWhite
      End With
    Next
  End If
End Sub
 
Last edited:
Upvote 0
Here is another macro that you can consider (it asks you the positions number and handles all the coloring as well)...
Thank you Rick Rothstein, for you kind help it is almost perfect when I run 1st time and for example input 11 it gives the result correct but when I run 2nd time and insert 5 it does not clear 1st once overlap and print also 2nd result

Please could you check it?

Thank you

Regards,
Kishan
 
Upvote 0
Thank you Rick Rothstein, for you kind help it is almost perfect when I run 1st time and for example input 11 it gives the result correct but when I run 2nd time and insert 5 it does not clear 1st once overlap and print also 2nd result
Sorry, I forgot to do that. Here is revised code which should do it all for you...
Code:
Sub CountsBeforeXs()
  Dim X As Long, Pos As Long, Count As Long, Cell As Range, Item As String
  Pos = Application.InputBox("Which position number (2 through 14)?", Type:=1)
  If Pos >= 2 And Pos <= 14 Then
    With Range("C3:T" & Cells(Rows.Count, "A").End(xlUp))
      .Interior.ColorIndex = xlColorIndexNone
      .Font.ColorIndex = vbBlack
      Intersect(.Rows, Columns("R:T")).ClearContents
    End With
    For Each Cell In Range(Cells(3, 2 + Pos), Cells(Rows.Count, 2 + Pos). _
                     End(xlUp)).SpecialCells(xlConstants, xlTextValues)
      Cell.Interior.Color = vbYellow
      Item = Cell.Offset(, -1).Value
      Count = 1
      For X = 2 To Pos - 1
        If Cell.Offset(, -X) = Item Then
          Count = Count + 1
        Else
          Exit For
        End If
      Next
      With Cell.Offset(, 1 - X).Resize(, X - 1)
        .Interior.ColorIndex = InStr("  1 X 2", Item)
        .Font.Color = vbWhite
      End With
      With Intersect(Cell.EntireRow, Columns("Q").Offset(, InStr("1X2", Item)))
        .Value = Count
        .Interior.ColorIndex = Cell.Offset(, -1).Interior.ColorIndex
        .Font.Color = vbWhite
      End With
    Next
  End If
End Sub
 
Upvote 0
Sorry, I forgot to do that. Here is revised code which should do it all for you...
Code:
Sub CountsBeforeXs()
  Dim X As Long, Pos As Long, Count As Long, Cell As Range, Item As String
  Pos = Application.InputBox("Which position number (2 through 14)?", Type:=1)
  If Pos >= 2 And Pos <= 14 Then
    With Range("C3:T" & Cells(Rows.Count, "A").End(xlUp))
      .Interior.ColorIndex = xlColorIndexNone
      .Font.ColorIndex = vbBlack
      Intersect(.Rows, Columns("R:T")).ClearContents
    End With
    For Each Cell In Range(Cells(3, 2 + Pos), Cells(Rows.Count, 2 + Pos). _
                     End(xlUp)).SpecialCells(xlConstants, xlTextValues)
      Cell.Interior.Color = vbYellow
      Item = Cell.Offset(, -1).Value
      Count = 1
      For X = 2 To Pos - 1
        If Cell.Offset(, -X) = Item Then
          Count = Count + 1
        Else
          Exit For
        End If
      Next
      With Cell.Offset(, 1 - X).Resize(, X - 1)
        .Interior.ColorIndex = InStr("  1 X 2", Item)
        .Font.Color = vbWhite
      End With
      With Intersect(Cell.EntireRow, Columns("Q").Offset(, InStr("1X2", Item)))
        .Value = Count
        .Interior.ColorIndex = Cell.Offset(, -1).Interior.ColorIndex
        .Font.Color = vbWhite
      End With
    Next
  End If
End Sub

<tbody> </tbody>
Thank you Rick Rothstein, finally you nail it now all is working 100% perfect. :)

Thank you to all, helped in this thread to solve my request

Have a great weekend

Regards,
Kishan
 
Upvote 0
Hi again,

All is working perfect as requested, sorry to bother you all again, just now come in my mind if macro could be modified which can have an option to count “Before1” X & 2 and “Before2” 1 & X will be grateful or other 2 independents macro which can do these jobs too.

Thank you

Regards
Kishan
 
Upvote 0
All is working perfect as requested, sorry to bother you all again, just now come in my mind if macro could be modified which can have an option to count “Before1” X & 2 and “Before2” 1 & X will be grateful or other 2 independents macro which can do these jobs too.
Sorry, I do not understand what you are asking for. Can you make a display of data and desired results like you did in Message #1 so I can see what you are trying to do?
 
Upvote 0
Sorry, I do not understand what you are asking for. Can you make a display of data and desired results like you did in Message #1 so I can see what you are trying to do?
Sure count result Before1

Book1
ABCDEFGHIJKLMNOPQRST
1YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14EmptyBefore "1" 11th PositionBefore "1" 11th PositionBefore "1" 11th Position
2YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14EmptyCount 1Count XCount 2
3201301/12/20132X2111X1112X11
4201302/12/201322X121XX21212X
5201303/12/201311111X1X11XX12
6201304/12/201311121112X111X11
7201305/12/20131X11X12X11111X2
8201306/12/201311122X111X111X1
9201307/12/201311X111121X11X11
10201308/12/2013121111211X11121
11201309/12/201322X2112X211X121
12201310/12/2013111X1221121X11
13201311/12/20131211X1111XX1XX
14201312/12/2013112X2212X11X211
15201313/12/20131X1X12221112222
16201314/12/2013XX11211222X111
17201315/12/201322222X1112X2X1
18201316/12/2013X111XXX111X122
19201317/12/201322X1212121X11X
20201318/12/2013X1121121X2X221
21201319/12/2013111212X22X12111
22201320/12/20132111X211X111221
Count 1-X-2 Before "1" 11th Pos


Sure count Result Before2

Book1
ABCDEFGHIJKLMNOPQRST
1YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14EmptyBefore "2" 11th PositionBefore "2" 11th PositionBefore "2" 11th Position
2YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14EmptyCount 1Count XCount 2
3201301/12/20132X2111X1112X113
4201302/12/201322X121XX21212X1
5201303/12/201311111X1X11XX12
6201304/12/201311121112X111X1
7201305/12/20131X11X12X11111X
8201306/12/201311122X111X111X
9201307/12/201311X111121X11X1
10201308/12/2013121111211X1112
11201309/12/201322X2112X211X12
12201310/12/2013111X1221121X11
13201311/12/20131211X1111XX1XX
14201312/12/2013112X2212X11X21
15201313/12/20131X1X1222111222
16201314/12/2013XX11211222X111
17201315/12/201322222X1112X2X1
18201316/12/2013X111XXX111X122
19201317/12/201322X1212121X11X
20201318/12/2013X1121121X2X221
21201319/12/2013111212X22X1211
22201320/12/20132111X211X11122
Count 1-X-2 Before "2" 11th Pos


Regards,
Kishan

<colgroup><col><col><col span="9"><col span="5"><col><col span="3"></colgroup><tbody>
</tbody>
 
Upvote 0
Oh, okay, I see, you want to be able to specify the character before which to count. The following macro will ask you two questions... the first is the position number (same question as my original code asked) and the second question will ask you for the character in that column to do the analysis for.
Code:
[table="width: 500"]
[tr]
	[td]Sub CountsBeforeCharacters()
  Dim X As Long, Pos As Long, Count As Long, Cell As Range, Item As String, Char As String
  Pos = Application.InputBox("Which position number (2 through 14)?", Type:=1)
  If Pos >= 2 And Pos <= 14 Then
    Char = InputBox("What character do you want to find prior to?")
    If Application.CountIf(Range("C3:P" & Cells(Rows.Count, "A").End(xlUp)), Char) Then
      With Range("C3:T" & Cells(Rows.Count, "A").End(xlUp))
        .Interior.ColorIndex = xlColorIndexNone
        .Font.ColorIndex = vbBlack
        Intersect(.Rows, Columns("R:T")).ClearContents
      End With
      Columns(2 + Pos).Replace Char, "#N/A", xlWhole
      For Each Cell In Columns(2 + Pos).SpecialCells(xlConstants, xlErrors)
        Cell.Interior.Color = vbYellow
        Item = Cell.Offset(, -1).Value
        Count = 1
        For X = 2 To Pos - 1
          If Cell.Offset(, -X) = Item Then
            Count = Count + 1
          Else
            Exit For
          End If
        Next
        With Cell.Offset(, 1 - X).Resize(, X - 1)
          .Interior.ColorIndex = InStr("  1 X 2", Item)
          .Font.Color = vbWhite
        End With
        With Intersect(Cell.EntireRow, Columns("Q").Offset(, InStr("1X2", Item)))
          .Value = Count
          .Interior.ColorIndex = Cell.Offset(, -1).Interior.ColorIndex
          .Font.Color = vbWhite
        End With
      Next
      Columns(2 + Pos).Replace "#N/A", Char, xlWhole
    End If
  End If
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,215,842
Messages
6,127,230
Members
449,371
Latest member
strawberrish

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