Rick, Will run your latest version and let you know. The first version worked very quickly on 100 cells in column K using 230 keywords on the list page
It *might*. Alternative code below should work with any number of key words.Did you say earlier your solution may limit the amount of key words that could be used?
Yes it is. Just add all your key words into column A of a sheet called 'Words' in the same workbook as the data. At the moment my code expects no blank cells within the data in column A. If there is, the code will take quite a while to run and everything in column K will get underlined. I could build in an extra check for blanks if required.Also, how would I add new key words or is it somehow pulling from the same "Words" page list I used with Rick's solution?
Sub UnderlineKeyWords_v2()
Dim AllMatches As Object
Dim itm As Variant, KeyWords As Variant, Keyword As Variant, Data As Variant
Dim tmp(1 To 2000) As Long
Dim DataRng As Range
Dim s As String
Dim i As Long, j As Long, k As Long
Const DataSht As String = "Data" '<- Name of sheet where underlining is done
Const myCol As String = "K" '<- Column of interest on DataSht
Application.ScreenUpdating = False
With Sheets("Words")
KeyWords = .Range("A1", .Cells(Rows.Count, "A").End(xlUp)).Value
End With
For i = 1 To UBound(KeyWords, 1)
KeyWords(i, 1) = "\b" & KeyWords(i, 1) & "\b"
Next i
With Sheets(DataSht)
.Columns(myCol).Font.Underline = False
Set DataRng = .Range(myCol & 1, .Range(myCol & .Rows.Count).End(xlUp))
End With
Data = DataRng.Value
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = True
For i = 1 To UBound(Data, 1)
Erase tmp
s = Data(i, 1)
k = -1
For Each Keyword In KeyWords
.Pattern = Keyword
Set AllMatches = .Execute(s)
For Each itm In AllMatches
k = k + 2
tmp(k) = itm.firstIndex + 1
tmp(k + 1) = itm.Length
Next itm
Next Keyword
With DataRng.Cells(i)
For j = 1 To k Step 2
.Characters(tmp(j), tmp(j + 1)).Font.Underline = True
Next j
End With
Next i
End With
Application.ScreenUpdating = True
End Sub
I thought that the comment in the code would make you realise that you had to edit this lineIt would be nice to see if I can get Peter's version 2 to work as well. I am getting run-time error 9 subscript out of range. When I choose debug, it selects the following line in the program - With Sheets(DataSht)
Const DataSht As String = "Data" '<- Name of sheet where underlining is done
For i = 1 To UBound(KeyWords, 1)
<del>KeyWords(i, 1) = "\b" & KeyWords(i, 1) & "\b"</del>
KeyWords(i, 1) = "\b" & KeyWords(i, 1) & "(?= |\b|$)"
Next i
Good news, it has been an interesting exercise - which is the reason many of us help here.BULLSEYE ...... Peter, You nailed it down real good with that last one. It is now perfect and works very fast as well. Does it all. Thank a million and fifty.
I'll second that. As usual, I have gained more knowledge & coding ideas by reading his suggestions/code.I also want to say once again thanks to Rick as well and his well functioning program ..
Thank you, it is always nice to have appreciative forum members.God Bless to you both and this Excel Forum has the best with your solutions, knowledge and brains for this sort of thing.