Highlight 2nd Occurrence of a Specific Character in Cell

chingching831

New Member
Joined
Jun 2, 2022
Messages
35
Office Version
  1. 2019
Platform
  1. Windows
Hi there,

Is there an excel formula or VBA code that can highlight the 2nd occurrence of a specific character in cell? For example, the letter "B" appears twice in the cell, how can I highlight the second "B" in blue?
BATBML

Thanks,
Ching
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi there,

Is there an excel formula or VBA code that can highlight the 2nd occurrence of a specific character in cell? For example, the letter "B" appears twice in the cell, how can I highlight the second "B" in blue?
BATBML

Thanks,
Ching
This give you the basics and can be altered depending on where the cells are and how many cells need to be formatted.

VBA Code:
Private Sub subHighlightCharacterInCell()
Dim i As Integer
Dim x As Integer
Dim rng As Range

  Set rng = Range("E5")

  For i = 1 To Len(rng.Value)
    If Mid(rng.Value, i, 1) = "B" Then
      x = x + 1
      If x = 2 Then
        With rng.Characters(i, 1).Font
          .Color = vbBlue
        End With
      End If
    End If
  Next i

End Sub
 
Upvote 0
I'm assuming a number of values in a column.

Before:

1730803514525.png


Code:

VBA Code:
Sub Highlight2nd()
  Dim RX As Object, M As Object
  Dim a As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "B"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      Set M = RX.Execute(a(i, 1))
      If M.Count > 1 Then .Cells(i).Characters(Start:=M(1).firstIndex + 1, Length:=1).Font.Color = vbBlue
    Next i
  End With
End Sub

After:

1730803572179.png
 
Upvote 0
This give you the basics and can be altered depending on where the cells are and how many cells need to be formatted.

VBA Code:
Private Sub subHighlightCharacterInCell()
Dim i As Integer
Dim x As Integer
Dim rng As Range

  Set rng = Range("E5")

  For i = 1 To Len(rng.Value)
    If Mid(rng.Value, i, 1) = "B" Then
      x = x + 1
      If x = 2 Then
        With rng.Characters(i, 1).Font
          .Color = vbBlue
        End With
      End If
    End If
  Next i

End Sub
Thanks a lot! May I know how this can be changed if I have a specific rows of data say for example T6:W6?
 
Upvote 0
I'm assuming a number of values in a column.

Before:

View attachment 118941

Code:

VBA Code:
Sub Highlight2nd()
  Dim RX As Object, M As Object
  Dim a As Variant
  Dim i As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "B"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      Set M = RX.Execute(a(i, 1))
      If M.Count > 1 Then .Cells(i).Characters(Start:=M(1).firstIndex + 1, Length:=1).Font.Color = vbBlue
    Next i
  End With
End Sub

After:

View attachment 118942
Thanks a lot! May I know how this can be changed if I have a specific rows of data say for example T6:W6?
 
Upvote 0
May I know how this can be changed if I have a specific rows of data say for example T6:W6?
The best way to deal with a different layout may depend on exactly what that layout is and what you are wanting to do.

Your statement above mentions rows (plural) of data but your example mentions just one (singular) row.
What is the exact circumstance? If there are multiple rows are all the rows together or are there other rows between that should not be processed? If multiple rows, do they all have the same number of columns to process? etc.
 
Upvote 0
Hi there,

Is there an excel formula or VBA code that can highlight the 2nd occurrence of a specific character in cell? For example, the letter "B" appears twice in the cell, how can I highlight the second "B" in blue?
BATBML
I will not use Regular Expressions anymore since it may give you a big trouble in the future.

If you still want to use it then simply
Code:
Sub test()
    Dim r As Range
    Const s$ = "B"
    With CreateObject("VBScript.RegExp")
        .Pattern = "^([^" & s & "]*" & s & "[^" & s & "]*)" & s & ".*$"
        For Each r In [T6:W6]
            If .test(r) Then r.Characters(Len(.Execute(r)(0).submatches(0)) + 1, Len(s)).Font.Color = vbBlue
        Next
    End With
End Sub
 
Upvote 0
Here's a simple one that doesn't use Regula Expression
Code:
Sub test()
    Dim r As Range
    For Each r In [T6:W6]
        If r Like "*B*B*" Then
            r.Characters(InStr(InStr(r, "B") + 1, r, "B"), 1).Font.Color = vbBlue
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,876
Messages
6,175,123
Members
452,614
Latest member
MRSWIN2709

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