VBA to Highlight Specific Phrase

chingching831

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

I am currently using the VBA code below to highlight specific text in a cell. However, it doesn't allow me to highlight a phrase with spacing in between (e.g. "live tour"). May I know how can I change the VBA so that I can highlight a phrase instead?


Sub HighlightStrings_CaseInsensitive_AllowForeignText_NotExactText()
Dim xHStr As String, xStrTmp As String
Dim xHStrLen As Long, xCount As Long, I As Long
Dim xCell As Range
Dim xArr

On Error Resume Next
xHStr = Application.InputBox("What are the words to highlight:", "Word Highlighter")
If TypeName(xHStr) <> "String" Then Exit Sub

Application.ScreenUpdating = Fals5
For Each xCell In Selection
Dim varWord As Variant
For Each varWord In Split(xHStr, Space$(1))
xHStrLen = Len(varWord)
xArr = Split(LCase(xCell.Value), LCase(varWord))
xCount = UBound(xArr)
If xCount > 0 Then
xStrTmp = ""
For I = 0 To xCount - 1
xStrTmp = xStrTmp & xArr(I)
xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.ColorIndex = 3
xCell.Characters(Len(xStrTmp) + 1, xHStrLen).Font.Bold = True
xStrTmp = xStrTmp & varWord
Next
End If
Next varWord
Next xCell
Application.ScreenUpdating = True
End Sub


Thanks,
Samantha
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:
VBA Code:
Sub chingching831_1()
Dim xFind As String
Dim regEx As Object, Matches As Object, M
Dim c As Range

xFind = Application.InputBox("What are the words to highlight:", "Word Highlighter", Type:=2)
If xFind = "" Then Exit Sub
Application.ScreenUpdating = False
        Set regEx = CreateObject("VBScript.RegExp")
        With regEx
                .Global = True
                .Pattern = "\b" & xFind & "\b"
            For Each c In Selection
                If .test(c) Then
                       Set Matches = .Execute(c)
                    For Each M In Matches
                         c.Characters(M.FirstIndex + 1, M.Length).Font.ColorIndex = 3
                         c.Characters(M.FirstIndex + 1, M.Length).Font.Bold = True
                    Next M
                End If
            Next
        End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub chingching831_1()
Dim xFind As String
Dim regEx As Object, Matches As Object, M
Dim c As Range

xFind = Application.InputBox("What are the words to highlight:", "Word Highlighter", Type:=2)
If xFind = "" Then Exit Sub
Application.ScreenUpdating = False
        Set regEx = CreateObject("VBScript.RegExp")
        With regEx
                .Global = True
                .Pattern = "\b" & xFind & "\b"
            For Each c In Selection
                If .test(c) Then
                       Set Matches = .Execute(c)
                    For Each M In Matches
                         c.Characters(M.FirstIndex + 1, M.Length).Font.ColorIndex = 3
                         c.Characters(M.FirstIndex + 1, M.Length).Font.Bold = True
                    Next M
                End If
            Next
        End With
Application.ScreenUpdating = True
End Sub
Works well, lots of thanks!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
Works well, lots of thanks!

The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,547
Members
452,652
Latest member
eduedu

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