We all know how to highlight the entire content of a cell in MS Excel. i.e. by going to the ribbon and changing the color in the “Font” section of the “Home” tab. Here is a macro that will enable you to highlight only specific key words within selected cells:
Sub Highlight()
Application.ScreenUpdating = False Dim A As Long Dim B As Long Dim C As Long Dim Range As Range Dim Word As String Dim Var As String Word = InputBox("Enter the text string to highlight") C = Len(Word) For Each Range In Selection With Range B = UBound(Split(Range.Value, Word)) If B > 0 Then Var = "" For A = 0 To B - 1 Var = Var & Split(Range.Value, Word)(A) .Characters(Start:=Len(Var) + 1, Length:=C).Font.ColorIndex = 6 Var = Var & Word Next End If End With Next Range Application.ScreenUpdating = True End Sub
Let’s test the macro on the data set below:
I will use the macro above to only highlight the word “Franco” in yellow:
Select the range that you want to apply the macro to. Upon running the macro, a window will pop up asking for the word to be highlighted. Enter the word and press “Ok”.
Here is the outcome:
Note that a variety of colors could be chosen for the purpose of highlighting selected words. In order to choose each color, change the ColorIndex (line 18 of the code above) based on the following illustration.
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
CategoriesArchives
June 2020
|