Forum Discussion

Betty's avatar
Betty
Copper Contributor
Mar 25, 2024
Solved

Highlight or Change the font color for only a specific word and not the entire cell in Excel?

Hi there.   This is my first post, and I hope someone can help me. I have a large spreadsheet I need to look through to find specific keywords and either highlight those keywords or change the fon...
  • NikolinoDE's avatar
    Mar 25, 2024

    Betty 

    You can highlight or change the font color for specific words within a cell in Excel using conditional formatting. Here's how you can do it:

    1. Select the Range: First, select the range of cells that you want to apply the conditional formatting to.

    2. Conditional Formatting Rules:

      • Go to the "Home" tab on the Excel ribbon.
      • Click on "Conditional Formatting" in the "Styles" group.
      • Choose "New Rule" from the dropdown menu.

    3. Create a Formula-Based Rule:

      • In the "New Formatting Rule" dialog box, select "Use a formula to determine which cells to format".
      • Enter the formula to check if the specific word exists in the cell. For example, if you want to highlight cells containing the word "Apple", you can use a formula like:

    =ISNUMBER(SEARCH("Apple", A1))

      • Replace "Apple" with the word you want to highlight and A1 with the cell reference containing the text.

    4. Format the Cells:

      • Click on the "Format" button to choose the formatting options. You can select the desired font color, fill color, or other formatting options.
      • Once you've configured the formatting options, click "OK" to apply them.

    5. Finalize the Rule:

      • Review the preview of the formatting in the "Preview" box.
      • Click "OK" to apply the conditional formatting rule to the selected range of cells.

    Now, Excel will automatically highlight or change the font color for any cell containing the specified word. Repeat the above steps for each word or phrase you want to highlight. You can also add multiple conditional formatting rules to achieve different formatting effects for different words.

     

    You can use VBA too.

    Here's a VBA solution to accomplish this task:

    Sub HighlightSpecificWords()
        Dim rng As Range
        Dim cell As Range
        Dim searchText As String
        
        ' Define the search text (word or phrase to highlight)
        searchText = "Apple"
        
        ' Set the range of cells to search
        Set rng = Range("A1:A100") ' Change this range to match your data
        
        ' Loop through each cell in the range
        For Each cell In rng
            ' Check if the search text exists in the cell
            If InStr(1, cell.Value, searchText, vbTextCompare) > 0 Then
                ' If found, apply formatting to the specific word
                HighlightSpecificWord cell, searchText
            End If
        Next cell
    End Sub
    
    Sub HighlightSpecificWord(cell As Range, searchText As String)
        Dim startPos As Integer
        Dim endPos As Integer
        Dim wordLength As Integer
        
        ' Find the starting position of the search text in the cell
        startPos = InStr(1, cell.Value, searchText, vbTextCompare)
        
        ' Calculate the ending position of the search text
        endPos = startPos + Len(searchText) - 1
        
        ' Highlight the specific word by changing the font color
        With cell.Characters(startPos, Len(searchText)).Font
            .Color = RGB(255, 0, 0) ' Change the RGB values to specify the desired font color
            ' You can also apply other formatting options here, such as bold, italic, etc.
        End With
    End Sub

    To use this VBA code:

    1. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
    2. Go to Insert > Module to insert a new module.
    3. Copy and paste the above code into the module window.
    4. Modify the searchText variable to specify the word or phrase you want to highlight.
    5. Adjust the range in the HighlightSpecificWords subroutine to match the range of cells you want to search.
    6. Close the VBA editor.
    7. Run the HighlightSpecificWords subroutine by pressing F5 or going to Run > Run Sub/UserForm.

    This VBA code will search through the specified range of cells, find the specific word or phrase, and highlight it by changing the font color. You can customize the formatting options according to your preferences by modifying the With statement inside the HighlightSpecificWord subroutine. The text was created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

     

    Was the answer useful? Mark as best response and like it!

    This will help all forum participants.

Resources