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 font color so they draw the reader's attention. I am unable to figure out how to do this automatically without highlighting the entire cell. Each cell has multiple lines of text, so I do not want to try to read through each cell to find the one word or key phrase to highlight or change the font size on my own. I need this to be an automatic action.

 

Any takers out there willing to give me a hand?

 

Thanks in advance.

  • 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.

15 Replies

  • ajt8888's avatar
    ajt8888
    Copper Contributor

    Well none of that worked for me. Can I ask how long ago this guidance was published i.e. has it been superceded?

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    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.

    • Lucy_Editor's avatar
      Lucy_Editor
      Copper Contributor

      NikolinoDE Hey there! This is only partially working for me. The formula=ISNUMBER(SEARCH("Apple", A1)) works to change the font color of the text in the way I want it to, but the problem is that if the word is present in the cell, any other text in the cell also changes to that color. For example, I want only the word "apple" to be gray. But with this formula, is "apple" is present, all of the text changes to gray. 

      If I remove the word "apple" then the text changes back to black. 

      I want all text to be black except the word "apple" even when that word is present. Is that possible? 

       

      Thanks in advance for your time!

    • Betty's avatar
      Betty
      Copper Contributor

      NikolinoDE 

       

      Thank you!

      This worked great. Since I am not a VBA master, would you be able to explain in laymens terms what each line of code means and how to modify it for any changes. Is this code only finding the first "word" or all "words"?

       

      Thanks again!

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor

        Betty 

        Let's break down the code and explain it in simply terms:

        vba code is untested, please backup your file.

        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

         

        Now, let's explain each part of the code and how to modify it:

        1. Sub HighlightSpecificWords():
          • This is the main subroutine that will be executed when you run the macro. It loops through each cell in the specified range and checks for the presence of the search text.
        2. Dim rng As Range / Dim cell As Range:
          • These lines declare two variables (rng and cell) to represent the range of cells to search and each individual cell within that range.
        3. Dim searchText As String:
          • This line declares a variable (searchText) to store the word or phrase you want to search for and highlight.
        4. searchText = "Apple":
          • Here, you define the search text. You can change "Apple" to any word or phrase you want to search for. You can also modify this line to read the search text from a specific cell, such as searchText = Range("B1").Value.
        5. Set rng = Range("A1:A100"):
          • This line sets the range of cells to search. You can change "A1:A100" to specify the range where your data is located.
        6. For Each cell In rng: ... Next cell:
          • This loop iterates through each cell in the specified range (rng).
        7. If InStr(1, cell.Value, searchText, vbTextCompare) > 0 Then:
          • This line checks if the search text (searchText) exists within the current cell (cell.Value). If it does, it proceeds to highlight the word.
        8. Sub HighlightSpecificWord(cell As Range, searchText As String):
          • This subroutine highlights the specific word within the cell.
        9. startPos = InStr(1, cell.Value, searchText, vbTextCompare):
          • This line finds the starting position of the search text within the cell.
        10. endPos = startPos + Len(searchText) - 1:
          • This line calculates the ending position of the search text.
        11. With cell.Characters(startPos, Len(searchText)).Font:
          • This block of code applies formatting to the specific word by changing its font color.
        12. .Color = RGB(255, 0, 0):
          • This line sets the font color to red (RGB values: 255 for red, 0 for green, 0 for blue). You can change these values to specify the desired font color.

         

        You can modify the code according to your needs by changing the search text, the range of cells to search, and the formatting options. Additionally, you can modify the code to read the search text from a specific cell by referencing that cell's address. For example, you can use searchText = Range("B1").Value to read the search text from cell B1. The text and steps were edited with the help of AI.

         

        My answers are voluntary and without guarantee!

         

        I hope that the text and the steps helped you a little to understand the VBA code.

Resources