VBA - How to Find Specific Text and Change the Font Color?

kschn

New Member
Joined
May 15, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with multiple tabs that I would like to have a VBA process find the underscores "_" and change the font color to white. This text exists on multiple tabs.

What is the most efficient VBA code to achieve this?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You could use conditional formatting.
This example select column J then goto conditional formatting.

Use
VBA Code:
=IFERROR(FIND("_",J1)>0,FALSE)

You can format the font color if true.
 
Upvote 0
You could use conditional formatting.
This example select column J then goto conditional formatting.

Use
VBA Code:
=IFERROR(FIND("_",J1)>0,FALSE)

You can format the font color if true.
I'm not sure if this will achieve the desired result. The "_" found in a cell will have other text in it. I am looking to change the color of just the "_" while the remaining text remains untouched.
 
Upvote 0
This will loop through the sheets and loop through column D to turn the "_" to white font, edit as required.

VBA Code:
Sub UnderScore()
    
    Dim sh          As Worksheet, rng As Range, c As Range, x
    
    For Each sh In Sheets
        With sh
            Set rng = .Range("D2:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            For Each c In rng.Cells
                x = InStr(c, "_")
                If x > 0 Then
                    c.Characters(Start:=x, Length:=1).Font.Color = vbWhite
                End If
            Next
        End With
        
    Next sh
    
End Sub
 
Upvote 0
This will loop through the sheets and loop through column D to turn the "_" to white font, edit as required.

VBA Code:
Sub UnderScore()
   
    Dim sh          As Worksheet, rng As Range, c As Range, x
   
    For Each sh In Sheets
        With sh
            Set rng = .Range("D2:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
            For Each c In rng.Cells
                x = InStr(c, "_")
                If x > 0 Then
                    c.Characters(Start:=x, Length:=1).Font.Color = vbWhite
                End If
            Next
        End With
       
    Next sh
   
End Sub
This is a great start but how would you adjust if the "_" can fall into multiple columns across various tabs of the workbook?
 
Upvote 0

Forum statistics

Threads
1,215,196
Messages
6,123,575
Members
449,108
Latest member
rache47

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