Regex: match and highlight string

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I've to match a string - cellz - in a range of cells.

The task is ok like this:

VBA Code:
Dim cellz As Variant
    For Each cellz In sheet1.Range("A2:A" & lr_col)
                With CreateObject("VBScript.RegExp")
                    .Global = True
                    .IgnoreCase = True
                    .Pattern = "\b" & cellz & "\b"
                For Each r In sheet2.Range("AG2:AG" & lr_ris)
                    If .test(r.Value) Then r.Interior.Color = vbRed
                Next r
        End With
    Next cellz

As you can see, I'm able of highlighting the cells where the string is found:

Code:
 If .test(r.Value) Then r.Interior.Color = vbRed

But I need something more: I need to write in yellow characters the string when found.

I mean: imagine cellz = "Paris"

Imagine cell where I'm looking for = "I don't live in Paris but I live in London"

I need like this: "I don't live in Paris but I live in London"

How can I perform the task?
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this.
Code:
Dim r As Range
Dim cellz As Range
Dim regEx As Object
Dim fnd As Object
Dim itm As Object

    Set regEx = CreateObject("VBScript.RegExp")
    
    For Each cellz In Sheet1.Range("A2:A" & lr_col)

        With regEx
            .Global = True
            .IgnoreCase = True
            .Pattern = "\b" & cellz & "\b"

            For Each r In Sheet2.Range("AG2:AG" & lr_ris)

                Set fnd = .Execute(r.Value)

                For Each itm In fnd
                    r.Characters(itm.firstindex + 1, itm.Length).Font.ColorIndex = 6
                Next itm

            Next r

        End With
    Next cellz
 
Upvote 0
Try this.
Code:
Dim r As Range
Dim cellz As Range
Dim regEx As Object
Dim fnd As Object
Dim itm As Object

    Set regEx = CreateObject("VBScript.RegExp")
 
    For Each cellz In Sheet1.Range("A2:A" & lr_col)

        With regEx
            .Global = True
            .IgnoreCase = True
            .Pattern = "\b" & cellz & "\b"

            For Each r In Sheet2.Range("AG2:AG" & lr_ris)

                Set fnd = .Execute(r.Value)

                For Each itm In fnd
                    r.Characters(itm.firstindex + 1, itm.Length).Font.ColorIndex = 6
                Next itm

            Next r

        End With
    Next cellz

Yes, it seems to work well.
And, in the same time, in order of painting the cell in red, is this ok? Or you suggest something different?

VBA Code:
                For Each itm In fnd
                    r.Characters(itm.firstindex + 1, itm.Length).Font.ColorIndex = 6
                    r.Interior.Color = vbRed
               Next itm
 
Upvote 0
I'm not sure I follow 100%.

Do you want to format the cell interior as red and the font of the found words as yellow?
 
Upvote 0
What you added should work then as if no matches are found then the loop will never be entered.
 
Upvote 0
What you added should work then as if no matches are found then the loop will never be entered.


Something is getting wrong in the following snippet:

VBA Code:
Dim cellw As Variant, r4 As Range, fnd4 As Object, itm4 As Object

    For Each cellw In ref.Range("B2:B" & lr_ref)
        If cellw <> "" And cellw <> "*CODE27*"  Then
                   
                    With CreateObject("VBScript.RegExp")
                        .Global = True
                        .IgnoreCase = True
                        .Pattern = "\b" & cellw & "\b"
                            For Each r4 In result.Range("AG2:AG" & lr_ris)
                                If r4.Interior.Color <> vbRed Then
                                    Set fnd4 = .Execute(r4.Value)
                                        For Each itm4 In fnd4
                                            r4.Characters(itm4.FirstIndex + 1, itm4.Length).Font.ColorIndex = 7
                                            r4.Interior.Color = vbYellow
                                        Next itm4
                                End If
                            Next r4
                    End With
        End If
    Next cellw

At the end of the process, In the analysed range I can find a lot of times in different rows, inside a text of about 150 characters:

VBA Code:
CODE27
with ColorIndex = 7 and interior.color = vbYellow

Even stranger, there aren't cellw at all containing CODE27 in any position.

What could it be happened?
 
Upvote 0
If you want search for the literal character '.' you'll need to escape it.

To do that you would need to replace '.' with '\.' in the pattern.
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,096
Members
452,542
Latest member
Bricklin

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