How to highlight specific text in Excel

In Excel, it may be easy for us to highlight the cells based on a specific text, but, here, I want to highlight a specific text within a cell to make it outstanding but not the entire cell. This maybe a troublesome for most of us. This article, I will talk about some tricks for solving this job in Excel.

Highlight a specific text within multiple cells with VBA code

For example, I have a range of text strings, and now, I want to highlight the specific text “sum” in these cells to get the result as following screenshot shown:

Macro with VBA code - Search a word
Macro with VBA code – Search a word

To highlight only part of text within a cell, the following VBA code can help you.

  1. Select the cells which you want to highlight the specific text, and then hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
  2. Click Insert > Module, and paste the following code in the Module Window.
Microsoft Excel - Visual Basic for Applications
Microsoft Excel – Visual Basic for Applications
Public Sub HighlightStrings()
    Application.ScreenUpdating = False
    Dim Rng As Range
    Dim cFnd As String
    Dim xTmp As String
    Dim x As Long
    Dim m As Long
    Dim y As Long
    cFnd = InputBox("Enter the text string to highlight")
    y = Len(cFnd)

    For Each Rng In Selection
        With Rng
            m = UBound(Split(Rng.Value, cFnd))
            If m > 0 Then
              xTmp = ""
                For x = 0 To m - 1
                    xTmp = xTmp & Split(Rng.Value, cFnd)(x)
                    .Characters(Start:=Len(xTmp) + 1, Length:=y).Font.ColorIndex = 3
                    xTmp = xTmp & cFnd
                Next
            End If
        End With
    Next Rng
    Application.ScreenUpdating = True
End Sub
  1. Then press F5 key to run this code, and a prompt box will pop out to remind you enter the text that you want to highlight only, see screenshot:
Microsoft Excel - Run HighlightStrings macro
Microsoft Excel – Run HighlightStrings macro
  1. And then click OK button, all the text you specified has been highlighted only within the cells, see screenshot:
Microsoft Excel - After run HighlightStrings macro
Microsoft Excel – After run HighlightStrings macro

Highlight specific text within a cell based on other text with VBA code

Here is another situation, I have two columns which the first column contains the text strings and the second column is the specific text, now, I need to highlight the relative text in the first column based on the specific text in the second column.

Microsoft Excel - Macro to highlight words in a column based on other text
Microsoft Excel – Macro to highlight words in a column based on other text
  1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
  2. Click Insert > Module, and paste the following code in the Module Window.
Sub Highlight()
    Dim xStr As String
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xChar As String
    Dim I As Long
    Dim J As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
LInput:
    Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    If xRg.Areas.Count > 1 Then
        MsgBox "not support multiple columns"
        GoTo LInput
    End If
    If xRg.Columns.Count <> 2 Then
        MsgBox "the selected range can only contain two columns "
        GoTo LInput
    End If
    For I = 0 To xRg.Rows.Count - 1
        xStr = xRg.Range("B1").Offset(I, 0).Value
        With xRg.Range("A1").Offset(I, 0)
            .Font.ColorIndex = 1
            For J = 1 To Len(.Text)
                If Mid(.Text, J, Len(xStr)) = xStr Then .Characters(J, Len(xStr)).Font.ColorIndex = 3
            Next
        End With
    Next I
End Sub
  1. After pasting the code, and press F5 key to run it, a prompt box will pop out to remind you select the data range which both contains the text string and specific text you want to highlight and based on, see screenshot:
Microsoft Excel: run macro Highlight for highlight a word from a list
Microsoft Excel: run macro Highlight for highlight a word from a list
  1. And then click OK button, all the corresponding text in the first column based on the specific text in the second column has been colored red as following screenshot:
Microsoft Excel: after run macro Highlight
Microsoft Excel: after run macro Highlight

Download the example for the download section. This file contains macros.

Enjoy Excel!

One thought on “How to highlight specific text in Excel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.