Range("K:K").Font.Underline = False
For Each Cell In Intersect(Columns("K"), ActiveSheet.UsedRange)
@mlien
Welcome to the MrExcel board!
It isn't clear where in Sheet1 you want to look for the words and underline them. However, Rick's code is looking and underlining in column K, so I suspect that may be your problem and where you could try amending the code to suit your particular circumstances.
Rich (BB code):Range("K:K").Font.Underline = False For Each Cell In Intersect(Columns("K"), ActiveSheet.UsedRange)
You would also need to ensure that Sheet1 is the active sheet when the code is run or else have the code specifically refer to that sheet.
Try this modification to those same two linesFor my next question, I want the macro to check for words in columns G, H and I. How do I change the code so that it checks these columns and underlines the words of my choice at the same time without having to use 3 different codes.
Range("G:I").Font.Underline = False
For Each Cell In Intersect(Columns("G:I"), ActiveSheet.UsedRange)
TryCan you help me modify the code below to that i works?
Sub UnderlineKeyWords()
Dim AllMatches As Object
Dim itm As Variant
Dim Cell As Range
Dim lr As Long
Const myCols As String = "G:I"
Application.ScreenUpdating = False
lr = Columns(myCols).Find(What:="*", After:=Columns(myCols).Cells(1), LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Columns(myCols).Resize(lr).Font.Underline = False
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = True
.Pattern = "\b(" & Join(Application.Transpose(Sheets("Words").Range("A1", Sheets("Words").Cells(Rows.Count, "A").End(xlUp)).Value), "|") & ")\b"
For Each Cell In Columns(myCols).Resize(lr).Cells
Set AllMatches = .Execute(Cell.Text)
For Each itm In AllMatches
Cell.Characters(itm.firstIndex + 1, itm.Length).Font.Underline = True
Next itm
Next Cell
End With
Application.ScreenUpdating = True
End Sub
When you get this message, if you click 'Debug' what line of the code is highlighted?.. I get this error message "Run-time error ´91´: Object variable or with block variable not set". Any ideas?