Excel VBA to Highlight Specific Text in a Cell (6 Ways)

Download Practice Workbook


We will use VBA codes to highlight specific text in the sample data set below.

Dataset for Excel VBA to Highlight Specific Text

 


Method 1 – VBA Code to Highlight a Single Specific Text in a Range of Cells in Excel (Case-Insensitive Match)

  • Develop a Macro to highlight a single specific text from a range of cells in the data set, with a case-insensitive match.
  • We will highlight the text “history” from the names of the books.
  • Enter the following VBA code.

VBA Code:

Sub Highlight_a_Single_Specific_Text_Case_Insensitive()

Text = InputBox("Enter the Specific Text: ")

Color_Code = Int(InputBox("Enter the Color Code: " + vbNewLine + "Enter 3 for Color Red." + vbNewLine + "Enter 5 for Color Blue." + vbNewLine + "Enter 6 for Color Yellow." + vbNewLine + "Enter 10 for Color Green."))

For i = 1 To Selection.Rows.Count
    For j = 1 To Selection.Columns.Count
        For k = 1 To Len(Selection.Cells(i, j))
            If LCase(Mid(Selection.Cells(i, j), k, Len(Text))) = LCase(Text) Then
                Selection.Cells(i, j).Characters(k, Len(Text)).Font.ColorIndex = Color_Code
            End If
        Next k
    Next j
Next i

End Sub
⧪ Note: This code produces a Macro called Highlight_a_Single_Specific_Text_Case_Insensitive.

Step-by-Step Procedure to Run the Code:

Step 1: Opening the VBA Window

  • Press ALT + F11 to open the VBA window.

Opening VBA Window to Highlight Specific Text in a Cell with VBA in Excel

Step 2: Inserting a New Module

  • In the VBA toolbar, go to the Insert > Module.
  • Click on Insert. A new module called “Module 1” will be inserted.

opening new module

Step 3: Entering the VBA Code

  • Enter the following VBA code into the new module.

VBA Code to Highlight Specific Text in a Cell with VBA in Excel

Step 4: Saving Macro-Enabled Workbook

  • Save the workbook as Excel Macro-Enabled Workbook.

Saving the Workbook to Highlight Specific Text in a Cell with VBA in Excel

Step 5: Running the VBA Code

  • Back on the worksheet, select the range to highlight the specific text. We have selected the column Book Name.
  • Press ALT+F8. A dialogue box named Macro will open.
  • Select Highlight_a_Single_Specific_Text_Case_Insensitive and click on Run.

running code for highlighting single specific text case insensitive

Step 6: Entering the Inputs

  • You will get two Input boxes. The first box will ask you to enter the specific text to highlight. We have entered “History”.

Entering Input to Highlight Specific Text in a Cell with VBA in Excel

  • The 2nd box will ask you to enter the color to highlight the text.
  • Enter 3 for a red, 5 for a blue, 6 for a yellow and 10 for green or enter any other color code according to the table provided by Microsoft.
  • We have entered 3 (Red).

entering code color to highlight text

Step 7: The Final Output

  • Click OK. You’ll get all the instances of the text “History” highlighted in red in your selected range.

Output to Highlight Specific Text in a Cell with VBA in ExcelThe code is case-insensitive. So, it will highlight all occurrences of the search term ‘history,’ including ‘History,’ ‘HISTORY,’ etc.

Read More: How to Highlight Selected Text in Excel (8 Ways)


Method 2 – VBA Code to Highlight a Single Specific Text in a Range of Cells in Excel (Case-Sensitive Match)

  • Create a new module and enter the following VBA code as shown in Method 1.

VBA Code:

Sub Highlight_a_Single_Specific_Text_Case_Sensitive()

Text = InputBox("Enter the Specific Text: ")

Color_Code = Int(InputBox("Enter the Color Code: " + vbNewLine + "Enter 3 for Color Red." + vbNewLine + "Enter 5 for Color Blue." + vbNewLine + "Enter 6 for Color Yellow." + vbNewLine + "Enter 10 for Color Green."))

For i = 1 To Selection.Rows.Count
    For j = 1 To Selection.Columns.Count
        For k = 1 To Len(Selection.Cells(i, j))
            If Mid(Selection.Cells(i, j), k, Len(Text)) = Text Then
                Selection.Cells(i, j).Characters(k, Len(Text)).Font.ColorIndex = Color_Code
            End If
        Next k
    Next j
Next i

End Sub
⧪ Note: This code produces a Macro called Highlight_a_Single_Specific_Texts_Case_Sensitive.

Steps to Run the Code:

  • Run the code as shown in Method 1.
  • Create a new module and insert the code.

VBA Code to Highlight Specific Text in a Cell with VBA in Excel

  • Save the workbook as Excel Macro-Enabled Workbook. Come back to your worksheet, select the range of cells and run the Macro named Highlight_a_Single_Specific_Text_Case_Sensitive.

Running Macro to Highlight Specific Text in a Cell with VBA in Excel

  • You will get two input boxes. Enter the text (“History”) in the 1st box.

entering specific text

  • Enter the color code (3 in this example) in the 2nd box.

entering color code

  • Click OK. You’ll find the instances of the text “history” highlighted red in your worksheet.

output of higlighting single specific text case sensitive

This is a case-sensitive code. So only the text “history” will be highlighted, not “History” or anything else.

Read More: Highlight Cells That Contain Text from a List in Excel (7 Easy Ways)


Method 3 – VBA Code to Highlight Multiple Specific Texts in a Range of Cells in Excel (Case-Insensitive Match)

  • Create a new module and enter the following VBA code.

VBA Code:

Sub Highlight_Multiple_Specific_Texts_Case_Insensitive()

Texts = InputBox("Enter the Specific Texts (Separated by Commas. No Space after the Commas): ")

Texts = Split(Texts, ",")

Color_Code = Int(InputBox("Enter the Color Code: " + vbNewLine + "Enter 3 for Color Red." + vbNewLine + "Enter 5 for Color Blue." + vbNewLine + "Enter 6 for Color Yellow." + vbNewLine + "Enter 10 for Color Green."))

For i = 1 To Selection.Rows.Count
    For j = 1 To Selection.Columns.Count
        For k = 1 To Len(Selection.Cells(i, j))
            For l = 0 To UBound(Texts)
                If LCase(Mid(Selection.Cells(i, j), k, Len(Texts(l)))) = LCase(Texts(l)) Then
                    Selection.Cells(i, j).Characters(k, Len(Texts(l))).Font.ColorIndex = Color_Code
                End If
            Next l
        Next k
    Next j
Next i

End Sub
⧪ Note: This code produces a Macro called Highlight_Multiple_Specific_Texts_Case_Insensitive.

Steps to Run the Code:

  • Run the code as shown in Method 1.
  • Create a new module and insert the code.

  • Save the workbook as Excel Macro-Enabled Workbook.
  • Back on your worksheet, select the range of cells and run the Macro called Highlight_Multiple_Specific_Texts_Case_Insensitive.

Running Macro to Highlight Specific Text in a Cell with VBA in Excel

  • You will get two input boxes. In the 1st box, enter the specific texts separated by commas (No space after the commas).
  • We have entered history,crime.

Entering Input to Highlight Specific Text in a Cell with VBA in Excel

  • Enter the color code (10 in this example) in the 2nd box.

Entering Input to Highlight Specific Text in a Cell with VBA in Excel

  • Click OK. You’ll find the instances of the texts “history” and “crime” highlighted green in your worksheet.

output highlighted multiple texts

This is a case-insensitive code. So, “history”, “History”, “crime”, “Crime” have been highlighted.


Similar Readings


Method 4 – VBA Code to Highlight Multiple Specific Texts in a Range of Cells in Excel (Case-Sensitive Match)

  • Create a new module and enter the following VBA code.

VBA Code: 

Sub Highlight_Multiple_Specific_Texts_Case_Sensitive()

Texts = InputBox("Enter the Specific Texts (Separated by Commas. No Space after the Commas): ")

Texts = Split(Texts, ",")

Color_Code = Int(InputBox("Enter the Color Code: " + vbNewLine + "Enter 3 for Color Red." + vbNewLine + "Enter 5 for Color Blue." + vbNewLine + "Enter 6 for Color Yellow." + vbNewLine + "Enter 10 for Color Green."))

For i = 1 To Selection.Rows.Count
    For j = 1 To Selection.Columns.Count
        For k = 1 To Len(Selection.Cells(i, j))
            For l = 0 To UBound(Texts)
                If Mid(Selection.Cells(i, j), k, Len(Texts(l))) = Texts(l) Then
                    Selection.Cells(i, j).Characters(k, Len(Texts(l))).Font.ColorIndex = Color_Code
                End If
            Next l
        Next k
    Next j
Next i

End Sub
⧪ Note: This code produces a Macro called Highlight_Multiple_Specific_Texts_Case_Sensitive.

Steps to Run the Code:

  • Run the code as shown in Method 1.
  • Create a new module and insert the code.

VBA Code to Highlight Specific Text in a Cell with VBA in Excel

  • Save the workbook as Excel Macro-Enabled Workbook, come back to your worksheet, select the range of cells and run the Macro called Highlight_Multiple_Specific_Texts_Case_Sensitive.

Running Macro to Highlight Specific Text in a Cell with VBA in Excel

  • You will get two input boxes. In the 1st box, enter the specific texts separated by commas (No space after the commas). We have entered history,crime.

Entering Input to Highlight Specific Text in a Cell with VBA in Excel

  • Enter the color code (5 in this example) in the 2nd box.

entering color code to highlighted text

  • Click OK. You’ll find the instances of the texts “history” and “crime” highlighted blue in your worksheet.

output of highlighting multiple text case sensitive

This is a case-sensitive code. So only “history”, “crime”, and “love” have been highlighted.


Method 5 – VBA Code to Highlight a Range of Specific Texts in a Range of Cells in Excel (Case-Insensitive Match)

In the sample dataset, there is an extra column called Texts to Highlight. We’ll match each text value from this column with the corresponding text value of the Book Name column. Also, highlight the portion in the book name if the corresponding text value is found in the name.

New Data Set to Highlight Specific Text in a Cell with VBA in Excel

  • Create a new module and insert the following VBA code.

VBA Code:

Sub Highlight_Range_of_Specific_Texts_Case_Insensitive()

Rng = InputBox("Enter the Range of the Specific Texts: ")

Color_Code = Int(InputBox("Enter the Color Code: " + vbNewLine + "Enter 3 for Color Red." + vbNewLine + "Enter 5 for Color Blue." + vbNewLine + "Enter 6 for Color Yellow." + vbNewLine + "Enter 10 for Color Green."))

Dim Start As Integer
Start = 1

For Each i In Range(Rng)
    For j = 1 To Len(Selection.Cells(Start, 1))
        If LCase(Mid(Selection.Cells(Start, 1), j, Len(i))) = LCase(i) Then
            Selection.Cells(Start, 1).Characters(j, Len(i)).Font.ColorIndex = Color_Code
        End If
    Next j
    Start = Start + 1
Next i

End Sub
⧪ Note: This code produces a Macro called Highlight_Range_of__Specific_Text_Case_Insensitive.

Steps to Run the Code:

  • Run the code as shown in Method 1.
  • Create a new module and insert the code.

VBA Code to Highlight Specific Text in a Cell with VBA in Excel

  • Save the workbook as Excel Macro-Enabled Workbook, come back to your worksheet, select the range of cells and run the Macro called Highlight_Range_of_Specific_Texts_Case_Insensitive.

running macro to highlight text from list case insensitive

  • Two input boxes will open. In the 1st box, enter the range of the specific texts to match. In this example, it’s F5:F14.

Entering Input to Highlight Specific Text in a Cell with VBA in Excel

  • Enter the color code (3 in this example) in the 2nd box.

Entering color code to Highlight Specific Text in a Cell with VBA in Excel

  • Click OK. You’ll find the instances highlighted in red where any portion of the book name matches the corresponding text value (With case-insensitive match).

output of highlighted multiple texts from range case insensitive

Read More: Excel VBA to Highlight Cell Based on Value (5 Examples)


Method 6 – VBA Code to Highlight a Range of Specific Texts in a Range of Cells in Excel (Case-Sensitive Match)

Create a new module and insert the following VBA code.

VBA Code:

Sub Highlight_Range_of_Specific_Texts_Case_Sensitive()

Rng = InputBox("Enter the Range of the Specific Texts: ")

Color_Code = Int(InputBox("Enter the Color Code: " + vbNewLine + "Enter 3 for Color Red." + vbNewLine + "Enter 5 for Color Blue." + vbNewLine + "Enter 6 for Color Yellow." + vbNewLine + "Enter 10 for Color Green."))

Dim Start As Integer
Start = 1

For Each i In Range(Rng)
    For j = 1 To Len(Selection.Cells(Start, 1))
        If Mid(Selection.Cells(Start, 1), j, Len(i)) = i Then
            Selection.Cells(Start, 1).Characters(j, Len(i)).Font.ColorIndex = Color_Code
        End If
    Next j
    Start = Start + 1
Next i

End Sub

⧪ Note: This code produces a Macro called Highlight_Range_of__Specific_Texts_Case_Sensitive.
  • Follow the steps shown in Method 1 and the output will be as shown in the image below.

Output to Highlight Specific Text in a Cell with VBA in Excel

  • Only the case-sensitive matches will be highlighted.

Related Readings

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

10 Comments
  1. how to insert the requested “text” & requested “color code” in the code itself .. without need of selection message box

    • Hi Mohammed, Thanks for your response. Insert the name of the text and the color code in the 2nd and 3rd lines of the codes directly.

  2. VBA code number 3 and 5 doesn’t work. I needed these codes but when I tried, 3rd one hangs and 5th one doesn’t work. Can you help

    • Hi Arjun, the codes are absolutely flawless and there is no reason for them to not work properly. Did you insert the inputs correctly?

  3. Reply
    Grijesh Prajapati Apr 19, 2022 at 9:32 PM

    HI,

    The above given VBA was very helpful,
    Only one thing, if the list of keywords has 2 or more matchable values separated with comma (,),
    How it can be the highlight

  4. Any way to also get a selection box for Bold, Italic, Underline, and also for size of text. I.e. If I wanted to make “History” Bold, Red, and size 16. Thanks!

    • Hello Scott,
      Thank you for sharing your problem. As per your query, you can simultaneously format specific text in Bold, Italic, Underline and even change Font Size with the following code.

      Sub Highlight_a_Single_Specific_Text_Case_Insensitive()

      Text = InputBox("Enter the Specific Text: ")

      Color_Code = Int(InputBox("Enter the Color Code: " + vbNewLine + "Enter 3 for Color Red." + vbNewLine + "Enter 5 for Color Blue." + vbNewLine + "Enter 6 for Color Yellow." + vbNewLine + "Enter 10 for Color Green."))

      For i = 1 To Selection.Rows.Count
      For j = 1 To Selection.Columns.Count
      For k = 1 To Len(Selection.Cells(i, j))
      If LCase(Mid(Selection.Cells(i, j), k, Len(Text))) = LCase(Text) Then
      Selection.Cells(i, j).Characters(k, Len(Text)).Font.ColorIndex = Color_Code
      Selection.Cells(i, j).Characters(k, Len(Text)).Font.Bold = True
      Selection.Cells(i, j).Characters(k, Len(Text)).Font.Italic = True
      Selection.Cells(i, j).Characters(k, Len(Text)).Font.Underline = True
      Selection.Cells(i, j).Characters(k, Len(Text)).Font.Size = 16
      End If
      Next k
      Next j
      Next i
      End Sub

      Apply this code to the selected cells of your dataset and you will get the desired output.
      I hope this solution will help you. Waiting to get your feedback.

      Regards,
      Guria
      ExcelDemy

  5. First website where the code actually worked thankyou so much!!

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo