Highlight 2nd Occurrence of a Specific Character in Cell

chingching831

New Member
Joined
Jun 2, 2022
Messages
33
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

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
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

Forum statistics

Threads
1,223,248
Messages
6,171,011
Members
452,374
Latest member
keccles

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