OK, test the macro below with a
copy of your workbook
Yes. I have used a sheet called 'Hidden' below. For this to work its best the list of words/phrases should be sorted from longest to shortest. The code below does the sorting so you can just add extra texts at the bottom of the table in the first column. Provided the table is set up correctly the length formula will automatically be inserted and my code will sort the table when it is run.
So, the Hidden sheet should be set up like this and made into a formal Excel table (post back if you need help with how to do that)
saeid025.xlsm |
---|
|
---|
| A | B |
---|
1 | Look For | Length |
---|
2 | long word | 9 |
---|
3 | word | 4 |
---|
4 | is | 2 |
---|
5 | | |
---|
6 | | |
---|
|
---|
This is the macro (again ask if you need help to implement)
VBA Code:
Sub Highlight_Strings()
Dim RX As Object, M As Object
Dim a As Variant
Dim i As Long
Set RX = CreateObject("VBScript.RegExp")
RX.Global = True
RX.IgnoreCase = True
Application.ScreenUpdating = False
With Sheets("Hidden").ListObjects(1).DataBodyRange
.Sort Key1:=.Columns(2), Order1:=xlDescending, Header:=xlNo
RX.Pattern = Join(Application.Transpose(.Columns(1)), "|")
End With
With Sheets("Sheet1")
With .Range("A2", .Range("A" & Rows.Count).End(xlUp))
a = .Value
For i = 1 To UBound(a)
For Each M In RX.Execute(a(i, 1))
With .Cells(i).Characters(Start:=M.firstindex + 1, Length:=M.Length).Font
.Color = RGB(255, 155, 0)
.Underline = xlUnderlineStyleSingle
End With
Next M
Next i
End With
End With
Application.ScreenUpdating = True
End Sub
Here is my test sheet before the code is used
saeid025.xlsm |
---|
|
---|
| A |
---|
1 | Data |
---|
2 | This is a word and this is a long word |
---|
3 | This WorD isn't case sensitive |
---|
4 | No special strings here |
---|
5 | The soldier had two swords |
---|
6 | |
---|
|
---|
.. and after the code has been run.
View attachment 118886