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
 
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"]
<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).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]

Thank you very much Rick Rothstein,

Both the VBA’s “CountsBeforeCharacters” & “CountsAfterCharacters” are working like a magic

I am very glad and I do appreciate a lot your kind help

Regards,
Kishan
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi,

I am bit confused I started this thread and got solution of my request as per opening post. After I request for code modification which also I got solved here. Honestly when work go on you come to know you need to be solved also in other way would be more better, request over request which I feel it is too much to ask in the same thread.

My question is do ask here new quiry, when this is solved twice by Rick Rothstein

Or I should open the new thread.

Please guide

Thank you

Regards,
Kishan
 
Upvote 0
My question is do ask here new quiry, when this is solved twice by Rick Rothstein

Or I should open the new thread.
If the new question is directly related to the thread's question, then you can post it here, otherwise start a new thread. The reason is for starting a new thread when the question is not directly related to the original question is to get more eyes on it. People who are not involved with the current thread (they decided they could not help at the time) won't see your new question whereas if you start a new thread, they will probably look at it and see if they can help or not. Also concerning the fact that I answered your previous two questions... remember that the people answering questions here are all volunteers, so you cannot count on us being available when you ask your next or follow-up questions, nor should you assume any one individual's knowledge of Excel is infinite in scope... while we may be able to provide an answer to one or two of your questions, we might not have a clue as to how to answer a different or distantly related question.
 
Upvote 0
If the new question is directly related to the thread's question, then you can post it here, otherwise start a new thread. The reason is for starting a new thread when the question is not directly related to the original question is to get more eyes on it.
Thank you Rick Rothstein, for you advice, as my query is related to question of this thread I will continue here.

People who are not involved with the current thread (they decided they could not help at the time) won't see your new question whereas if you start a new thread, they will probably look at it and see if they can help or not.
You are correct.

Also concerning the fact that I answered your previous two questions... remember that the people answering questions here are all volunteers
Yes I am really great full to all people are answering and helping voluntarily, and that’s why I feel bad asking again and again giving them troubles I would have thought toughly before asking

so you cannot count on us being available when you ask your next or follow-up questions, nor should you assume any one individual's knowledge of Excel is infinite in scope... while we may be able to provide an answer to one or two of your questions, we might not have a clue as to how to answer a different or distantly related question.
I appreciate you for clarifying my question next time I will precise my query.

Here is my last request to this thread

Could it be possible to count both sides before & after the selected character as shown below?


Book1
ABCDEFGHIJKLMNOPQRSTUVWX
1YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14EmCount Before CharCount Before CharCount Before CharEmCount After CharCount After CharCount After Char
2YearDateP1P2P3P4P5P6P7P8P9P10P11P12P13P14Em1XOr 2Em1XOr 2
3201301/12/20132X2111X1112x11
4201302/12/201322X121XX21212X11
5201303/12/201311111X1X11Xx12
6201304/12/201311121112X111X1
7201305/12/20131X11X12X11111X
8201306/12/201311122X111X111X11
9201307/12/201311X111121X11X1
10201308/12/2013121111211X1112
11201309/12/201322X2112X211x12
12201310/12/2013111X1221121x11
13201311/12/20131211X1111XX1XX
14201312/12/2013112X2212X11x2111
15201313/12/20131X1X1222111222
16201314/12/2013XX11211222X11122
17201315/12/201322222X1112X2X141
18201316/12/2013X111XXX111X122
19201317/12/201322X1212121X11X11
20201318/12/2013X1121121X2X221
21201319/12/2013111212X22X1211
22201320/12/20132111X211X11122
Count Both Side Of Selecte Char


Please help

Thanks And Regards,
Kishan
 
Upvote 0
Could it be possible to count both sides before & after the selected character as shown below?
Give this code a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub CountsBeforeAndAfterCharacters()
  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:X" & Cells(Rows.Count, "A").End(xlUp))
        .Interior.ColorIndex = xlColorIndexNone
        .Font.ColorIndex = vbBlack
        Intersect(.Rows, Columns("V:X")).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
      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("U").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
Give this code a try...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub CountsBeforeAndAfterCharacters()
  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:X" & Cells(Rows.Count, "A").End(xlUp))
        .Interior.ColorIndex = xlColorIndexNone
        .Font.ColorIndex = vbBlack
        Intersect(.Rows, Columns("V:X")).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
      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("U").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]
Thank you Rick Rothstein, for you help once again for the time you spent to solve and giving an ideal solution I appreciate your help

Please could you check?
1-Column R:T does not erase previous data
2-when I enter position 14 data do not present correctly.

Thanks And Regards,
Moti
 
Upvote 0
Thank you Rick Rothstein, I just change "If Pos >= 2 And Pos <= 14 Then" to "If Pos >= 2 And Pos <= 13 Then" and "Intersect(.Rows, Columns("V:X")).ClearContents" to "Intersect(.Rows, Columns("R:X")).ClearContents" and it is working fine.

I appreciate your time and for your great help

Regards,
Kishan
 
Upvote 0
Thank you Rick Rothstein, I just change "If Pos >= 2 And Pos <= 14 Then" to "If Pos >= 2 And Pos <= 13 Then" and "Intersect(.Rows, Columns("V:X")).ClearContents" to "Intersect(.Rows, Columns("R:X")).ClearContents" and it is working fine.
You are quite welcome. As for the things you had to change... I posted the code just before going to sleep and missed them when I combined to the two codes I had posted previously... I am glad you were able to figure out what needed to be changed on your own (congratulations on being able to do that).
 
Upvote 0
You are quite welcome. As for the things you had to change... I posted the code just before going to sleep and missed them when I combined to the two codes I had posted previously... I am glad you were able to figure out what needed to be changed on your own (congratulations on being able to do that).

Thank you Rick Rothstein, for congratulating to me, tell you the truth when I modified and it worked I felt very happy my self even knowing I did nothing.

You are very generous.

Have a great weekend

Regards,
kishan
 
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