Apply font color to all matches (regex) under the worksheet

bnpdx

New Member
Joined
Jul 1, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to create VBA macro that would color all text matches (using regex search) using red font.

I found the below VBA code online.

The problem is, I need it to look for matches across the entire worksheet and not only in cell A1.

Can you help me extend the functionality to include the entire worksheet?

I'm sorry for not being that VBA-savvy ;(

Thank you in advance!!


VBA Code:
Sub RegExpRed()

    Dim objRegex As Object
    Dim RegMC As Object
    Dim RegM As Object

    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Global = True
        .Pattern = "\w+:"
        If .test(Cells(1, 1).Value) Then
            Set RegMC = .Execute(Cells(1, 1).Value)
            For Each RegM In RegMC
                Cells(1, 1).Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = vbRed
            Next
        End If
    End With

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Changes in red, but caveat emptor I have not tested this.

Rich (BB code):
Sub RegExpRed()

    Dim objRegex As Object
    Dim RegMC As Object
    Dim RegM As Object
    Dim Cell As Range

    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Global = True
        .Pattern = "\w+:"
        For Each Cell In UsedRange
           If .test(Cell.Value) Then
               Set RegMC = .Execute(Cell.Value)
               For Each RegM In RegMC
                   Cell.Characters(RegM.FirstIndex + 1, RegM.Length).Font.Color = vbRed
               Next
           End If
        Next Cell
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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