Highlight a cell / VBA

gripper

Board Regular
Joined
Oct 29, 2002
Messages
176
I am trying to create a filtering code to highlight cells if a particular word is in the string of that cell.

I found this code that allows you to highlight the column and then run the macro and it will change the font color of the actual word matched; not the entire string. I would like to change this to highlight the cell in yellow if within that cell it has a match within the string.

I am wondering is it possible instead of adding each word to the VBA code to instead reference a text file or another excel workbook with the words that need filtering. I have about 60 words I need to screen on each excel sheet I open and run the macro and since these words are updated, added or deleted it may be easier just to have the info in a central location.

Here is the code
Code:
Public Sub HighlightCodes()
        ' Select Cells to be highlighted and Run this Sub.
         Dim Codes(1 To 8)
         Dim Rng As Range
         Dim i As Long
         Dim StartPos As Long
               Codes(1) = "car."
               Codes(2) = "motorcycle"
               Codes(3) = "scooter"
               Codes(4) = "airplane"
               Codes(5) = "skateboard"
               Codes(6) = "bicycle"
               Codes(7) = "motorhome"
               Codes(8) = "boat"

         For Each Rng In Selection.Cells
               For i = 1 To 8
         StartPos = InStr(Rng.Value, Codes(i))
         If StartPos > 0 Then Rng.Characters(StartPos, Len(Codes(i))).Font.ColorIndex = 15
      Next i
   Next Rng
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I am trying to create a filtering code to highlight cells if a particular word is in the string of that cell.

I found this code that allows you to highlight the column and then run the macro and it will change the font color of the actual word matched; not the entire string. I would like to change this to highlight the cell in yellow if within that cell it has a match within the string.

I am wondering is it possible instead of adding each word to the VBA code to instead reference a text file or another excel workbook with the words that need filtering. I have about 60 words I need to screen on each excel sheet I open and run the macro and since these words are updated, added or deleted it may be easier just to have the info in a central location.

Here is the code
Code:
Public Sub HighlightCodes()
        ' Select Cells to be highlighted and Run this Sub.
         Dim Codes(1 To 8)
         Dim Rng As Range
         Dim i As Long
         Dim StartPos As Long
               Codes(1) = "car."
               Codes(2) = "motorcycle"
               Codes(3) = "scooter"
               Codes(4) = "airplane"
               Codes(5) = "skateboard"
               Codes(6) = "bicycle"
               Codes(7) = "motorhome"
               Codes(8) = "boat"

         For Each Rng In Selection.Cells
               For i = 1 To 8
         StartPos = InStr(Rng.Value, Codes(i))
         If StartPos > 0 Then Rng.Characters(StartPos, Len(Codes(i))).Font.ColorIndex = 15
      Next i
   Next Rng
End Sub

For the first question, maybe:

Code:
Public Sub HighlightCodes()
        ' Select Cells to be highlighted and Run this Sub.
         Dim Codes(1 To 8)
         Dim Rng As Range
         Dim i As Long
         Dim StartPos As Long
               Codes(1) = "car."
               Codes(2) = "motorcycle"
               Codes(3) = "scooter"
               Codes(4) = "airplane"
               Codes(5) = "skateboard"
               Codes(6) = "bicycle"
               Codes(7) = "motorhome"
               Codes(8) = "boat"

         For Each Rng In Selection.Cells
               For i = 1 To 8
                    StartPos = InStr(Rng.Value, Codes(i))
                       If StartPos > 0 Then Rng.Interior.ColorIndex = 6
               Next i
         Next Rng

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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