automatically underline certain key words in all cells in a column

skyport

Active Member
Joined
Aug 3, 2014
Messages
374
Was hoping there is a chance this can actually be done with some sort of VBA -automatically underline certain key words in all cells in a column
 
@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.
 
Last edited:
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
@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.

Thanks for the input I will give this a try!
 
Upvote 0
Thank you so much for your help Peter and Rick! I changed the column to "G" and it worked flawlessly.

For 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.
 
Upvote 0
For 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.
Try this modification to those same two lines
Code:
Range("G:I").Font.Underline = False
For Each Cell In Intersect(Columns("G:I"), ActiveSheet.UsedRange)
 
Last edited:
Upvote 0
Hi Peter,

Can you help me modify the code below to that i works? Thanks!


Sub UnderlineKeyWords()
Dim AllMatches As Object
Dim itm As Variant
Dim Cell As Range


Const myCol As String = "G" '<- Column of interest


Application.ScreenUpdating = False
Columns(myCol).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 Range(myCol & 1, Range(myCol & Rows.Count).End(xlUp))
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
 
Upvote 0
When posting code, please use code tags to preserve the indentation which makes the code easier to read, debug and format when pasted into our own code window. My signature block below explains how.


Can you help me modify the code below to that i works?
Try
Code:
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
 
Upvote 0
Hi Peter,

I ran the macro and I get this error message "Run-time error ´91´: Object variable or with block variable not set". Any ideas?
 
Upvote 0
.. I get this error message "Run-time error ´91´: Object variable or with block variable not set". Any ideas?
When you get this message, if you click 'Debug' what line of the code is highlighted?
 
Upvote 0
That would seem to me to indicate that you don't have anything in columns G:I, which is where in post #53 you said your data was.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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