VBA - Highlight specific key words

Cruiser69

Board Regular
Joined
Mar 12, 2018
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hi all.

I use the following code to highlight in red, certain keywords which appear in a sheet.
There are many more, but I have just shortened it for this purpose.
It works well, but it will highlight anything which contains the word e.g TEA
Like STEAM MOP.
Or BOOKCASE and not just BOOK

Is there a way to just highlight the specific word I have in the Comments




VBA Code:
Dim ws As Worksheet

Dim Match As Range

Dim Comment() As String

Set ws = ActiveWorkbook.Worksheets(1)

ReDim Comment(3)

Comment(0) = "TEA"

Comment(1) = "COFFEE"

Comment(2) = "BOOK"



For i = LBound(Comment) To UBound(Comment)

Set Match = ws.Cells.Find(What:=Comment(i), LookIn:=xlValues, _

LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _

MatchCase:=False, SearchFormat:=False)



If Not Match Is Nothing Then

FirstAddress = Match.Address

Do

sPos = InStr(1, Match.Value, Comment(i))

sLen = Len(Comment(i))

Match.Characters(Start:=sPos, Length:=sLen).Font.Color = RGB(255, 0, 0) ‘Red

Match.Interior.Color = RGB(153, 255, 153) 'Green

Set Match = ws.Cells.FindNext(Match)

Loop While Not Match Is Nothing And Match.Address <> FirstAddress

End If

Next



Thanks for looking

Regards,

Graham
 

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.
Maybe...
Code:
LookAt:=xlWhole
and/or...
Code:
If (Not Match Is Nothing) And (Len(Match) = Len(Comment(i)) Then
HTH. Dave
 
Upvote 0
Hi Dave

The first code did not highlight any words.

The second came up with Compile error Expected: )

Regards,

Graham
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA Highlight specific key words
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Whoops. Missed a bracket...
Code:
If (Not Match Is Nothing) And (Len(Match) = Len(Comment(i))) Then
Dave
 
Upvote 0
Hi Fluff.

I wasn't aware of that.
The link below is where I posted the same question

 
Upvote 0
Here's what code I would use. Seems to work fairly fast and only highlights the exact word. Dave
Code:
Sub Test()
Dim ws As Worksheet
Dim Rng As Range
Dim Comment As Variant

Set ws = ActiveWorkbook.Worksheets(1)
Comment = Array("TEA", "COFFEE", "BOOK")
On Error GoTo ErFix
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlManual

For Each Rng In ws.UsedRange
If Rng.Text <> vbNullString Then
For i = LBound(Comment) To UBound(Comment)
If LCase(Rng.Text) = LCase(Comment(i)) Then
Rng.Interior.Color = RGB(153, 255, 153) 'Green
End If
Next i
End If
Next Rng
ErFix:
If Err.Number <> 0 Then
MsgBox "Error"
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic
End Sub
 
Upvote 0
Hi Dave.

I've tried it but can't get it to work. Does it make a difference if the text is in upper or lower case. It will always be in uppercase.
 
Upvote 0
Hmmm. I trialed the code with success before posting? It doesn't matter upper or lowercase. The LCase in the comparative test is used just to compare apples to apples so that case changes between the compared strings doesn't matter. When U say can't get it to work, what does that mean? It errors? Does nothing? Wrong output? Depending on the size of your sheet's used range, it may take some time to run. Dave
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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