Search a string of text and highlight specific words within the string that match any cell in a reference column

mp1021257

New Member
Joined
Mar 2, 2016
Messages
19
Hi,

I am trying to write a macro that will search three strings of text in Range (E6:E8), by comparing them to cells in a different worksheet. The cells in the other worksheet are in Range (C4:C100).

For example, lets say in cell E6 there is a list of names: Tom, Matt, Bob, Jim, Kathy, etc.

In cells C4:C100, I have another list of names:

Jack
John
Jimmy
Tom
Joel
Bob


After running the macro, I would like the names Tom and Bob (the names from E6 that match up with cells in C4:C100), to be bolded and red. Specifically: Tom, Matt, Bob, Jim, Kathy, etc.


Thanks for the help.

Best,
Mark
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You don't provide much detail and your example is limited. The code below is designed to address you singular example. That is, the string in any cell is characterized by sub-strings separated by comma and space delimiters. I'll assume the strings are on the activesheet when the code is run, and the list is on Sheet2 - change to suit. Also assume there are no duplicate sub-strings in a cell.

Code:
Sub HiLiteMatches()
Dim Sht As Worksheet, S As Variant, R As Range, c As Range, i As Long
Set Sht = Sheets("Sheet2")
Set R = Sht.Range("C4:C100")
Application.ScreenUpdating = False
For Each c In ActiveSheet.Range("E6:E8")
       S = Split(c.Value, ", ")
       For i = LBound(S) To UBound(S)
              If Not IsError(Application.Match(S(i), R, 0)) Then
                     With c.Characters(InStr(c.Value, S(i)), Len(S(i))).Font
                            .Bold = True
                            .Color = vbRed
                     End With
              End If
       Next i
Next c
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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