Highlight 2nd Occurrence of a Specific Character in Cell

chingching831

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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

chingching831,​


If the cell is Numeric or formula, you can't change the cell partially, so you will need to convert the cell as text sting.
 
Upvote 0
Hi All,
I revised the code to below. I wonder how I can add a second condition here, so that when the cell contains (TBS) OR (ATB), OR (AB) the first occurrence of "B" will also be highlighted in blue colour?

VBA Code:
Sub Highlight_2nd_Occurence_Series()
    Dim r As Range
    For Each r In [T12:W12]
        If r Like "*B*B*" Then
            r.Characters(InStr(InStr(r, "B") + 1, r, "B"), 1).Font.Color = RGB(68, 114, 196)
        End If
    Next
End Sub
 
Upvote 0
Code:
        If r Like "*B*" Then
            r.Characters(Application.Max(InStr(r, "B"), InStr(InStr(r, "B") + 1, r, "B")), 1).Font.Color = RGB(68, 114, 196)
        End If
 
Upvote 0
Is this what you mean?

VBA Code:
Sub Highlight_Special()
  Dim r As Range
  
  For Each r In [T12:W12]
    If r Like "*B*B*" Then
      r.Characters(InStr(InStr(r, "B") + 1, r, "B"), 1).Font.Color = RGB(68, 114, 196)
    ElseIf r.Value = "TBS" Or r.Value = "ATB" Or r.Value = "AB" Then
      r.Characters(InStr(r, "B"), 1).Font.Color = RGB(68, 114, 196)
    End If
  Next
End Sub
 
Upvote 0
Thanks Peter! This version only highlights my first condition, but not the 2nd.

1st condition
Highlight "B" if it occurs the 2nd time in the cell

e.g.
1738652381150.png


2nd condition
Highlight "B" if it appears in any of the following forms
1738652673871.png
 
Upvote 0
Are you intenstonally ignoring my posts?
If so, why you use my code?
 
Upvote 0
Are you intenstonally ignoring my posts?
If so, why you use my code?
Sorry Fuji, I missed out your reply. But really appreciate for your help as it assists my another scenario that highlights the 2nd occurrence of B and 1st occurrence of B if the first condition doesnt meet! Lots of thanks!
 
Upvote 0
but not the 2nd.
That's because I didn't understand that the "AB" etc would be in the cell :cool: .

So, just checking the second condition, you want to highlight if (TBS) OR (ATB), OR (AB) is in the cell. Is it possible that a cell in your data could contain something else with a "B" that should not be coloured like, for example, this?

Jun 24, 2016 (CBW)
 
Upvote 0

Forum statistics

Threads
1,226,179
Messages
6,189,476
Members
453,549
Latest member
MBenedikt

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