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
 
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"]
<tbody>[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]
</tbody>[/TABLE]

Rick Rothstein, I cannot believe it is done. You are amazing I am very happy really I was not expecting such a nice solution

Thank you very much for your kind help and making life easier

Regards,
Kishan:)
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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]

Hello Rick Rothstein, "VBA CountsBeforeCharacters" is working perfectly.

May I ask you one more favour please could you make one another VBA that can "Count After Characters"

For example in the #post19 "2nd image Sure count Result Before2" count 1 X or 2 before the "character 2" in this case count after the "character 2" will be great help of you.

Thank you

Regards,
Kishan
 
Upvote 0
Last edited:
Upvote 0
Since this is an international forum, help will come from different time zones, those who helped you earlier might be in sleep now,
It is always better to bump up the existing thread, since those who helped already, know the background of the requirement and help further

Thank you for understanding
 
Last edited:
Upvote 0
Hello Rick Rothstein, "VBA CountsBeforeCharacters" is working perfectly.

May I ask you one more favour please could you make one another VBA that can "Count After Characters"

For example in the #post19 "2nd image Sure count Result Before2" count 1 X or 2 before the "character 2" in this case count after the "character 2" will be great help of you.

Thank you

Regards,
Kishan


Do you want Count after the 11th position? (ie) only in P12-P14?
 
Upvote 0
Since this is an international forum, help will come from different time zones, those who helped you earlier might be in sleep now,

Hi sanrv1f, you are correct I never thought about time zones good point to know.

Thank you for your help and I feel really sorry all about.

Regards,
Kishan
 
Last edited:
Upvote 0
Do you want Count after the 11th position? (ie) only in P12-P14?
Hi sanrv1f,

No I want to count from P1 To P13 (I have chosen 11th position just for example but it can be any P1 to P13) first step VBA give an input option to select the position, and than character to choose.

Regards,
Kishan
 
Last edited:
Upvote 0
Do you want Count after the 11th position? (ie) only in P12-P14?
Hi sanrv1f,

Reading your question again I realized, yes if 11th position is selected than as you say is correct “(ie) only in P12-P14?” and If for Example 4th position is selected than P5-P14 is correct

Regards,
Kishan
 
Upvote 0
Hello Rick Rothstein, "VBA CountsBeforeCharacters" is working perfectly.

May I ask you one more favour please could you make one another VBA that can "Count After Characters"

For example in the #post19 "2nd image Sure count Result Before2" count 1 X or 2 before the "character 2" in this case count after the "character 2" will be great help of you.
I think this does what you want (it is a modification of the code I gave you earlier where it counts and highlights after the position number and character you specify)...
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).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,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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