Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Find and Color Cells in Excel with VBA

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To find and color a cell in Excel with VBA, use the Find method to locate the value, then apply a background color via the Interior.Color property. The shortest working pattern is: Set foundCell = Range("A1:Z100").Find(What:="target", LookIn:=xlValues, LookAt:=xlWhole), then if found, foundCell.Interior.Color = RGB(255, 255, 0). This guide walks through the exact steps, common pitfalls, and fixes so you can automate highlighting without breaking your workbook.

What You'll Need

  • Microsoft Excel (desktop version for Windows or Mac). VBA is not available in Excel for the web or most mobile versions.
  • A workbook saved as .xlsm (macro-enabled) if you plan to keep the code.
  • Basic familiarity with the VBA editor: press Alt + F11 (Windows) or Fn + Option + F11 (Mac) to open it.
  • The Developer tab enabled: File > Options > Customize Ribbon > check Developer. On Mac: Excel > Preferences > Ribbon & Toolbar.

Step-by-Step

  1. Open the VBA editor. Press Alt + F11 (Windows) or Fn + Option + F11 (Mac). If the Developer tab is visible, you can also click Developer > Visual Basic. Menu labels can differ slightly by Excel version and platform.
  2. Insert a new module. In the editor, go to Insert > Module. A blank code window appears. This is where your macro will live.
  3. Write the search-and-color macro. Paste the following code, adjusting the range and search term to match your sheet:
    Sub FindAndColor()
        Dim ws As Worksheet
        Dim searchRange As Range
        Dim foundCell As Range
        Dim searchTerm As String
        
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        Set searchRange = ws.Range("A1:Z100")
        searchTerm = "Pending"
        
        Set foundCell = searchRange.Find(What:=searchTerm, _
            LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
        
        If Not foundCell Is Nothing Then
            foundCell.Interior.Color = RGB(255, 255, 0)  ' yellow
        Else
            MsgBox "Value not found."
        End If
    End Sub
  4. Understand the key arguments. LookIn:=xlValues searches displayed values, not formulas. LookAt:=xlWhole matches the entire cell; use xlPart to match substrings. MatchCase:=False makes the search case-insensitive.
  5. Run the macro. Press F5 while the cursor is inside the subroutine, or go back to Excel and use Developer > Macros, select FindAndColor, and click Run. The first matching cell gets a yellow fill.
  6. Color all matches (optional). To highlight every occurrence, wrap the search in a loop:
    Sub FindAndColorAll()
        Dim ws As Worksheet, searchRange As Range, foundCell As Range
        Dim firstAddress As String
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        Set searchRange = ws.Range("A1:Z100")
        
        Set foundCell = searchRange.Find(What:="Pending", LookIn:=xlValues, LookAt:=xlWhole)
        If Not foundCell Is Nothing Then
            firstAddress = foundCell.Address
            Do
                foundCell.Interior.Color = RGB(255, 255, 0)
                Set foundCell = searchRange.FindNext(foundCell)
            Loop While Not foundCell Is Nothing And foundCell.Address <> firstAddress
        End If
    End Sub
    The firstAddress check prevents an infinite loop.
  7. Clear the color when needed. Use foundCell.Interior.ColorIndex = xlNone to remove the fill. To clear all highlights in a range, loop through cells or use searchRange.Interior.ColorIndex = xlNone.
  8. Save the workbook as macro-enabled. Go to File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). If you save as .xlsx, the macro is stripped out.

Common Mistakes

  • Forgetting to check for Nothing. If Find returns nothing and you immediately set .Interior.Color, you get a runtime error 91. Always test If Not foundCell Is Nothing first.
  • Searching the wrong sheet. Range("A1:Z100") without a worksheet qualifier refers to the active sheet, which may not be the one you expect. Qualify with ws.Range.
  • Using LookAt:=xlWhole for partial matches. Searching for "pen" with xlWhole will not find "Pending". Switch to xlPart for substrings.
  • Assuming Find searches formulas. By default it searches values. If your cell contains a formula that returns "Pending", LookIn:=xlValues works; if you need to search the formula text itself, use LookIn:=xlFormulas.
  • Not resetting Find settings. Excel remembers the last Find parameters. If a previous search used MatchCase:=True, your new search may behave unexpectedly. Specify all arguments explicitly each time.
  • Saving as .xlsx. The macro disappears on save. Use .xlsm.

Tips

  • Use RGB() for precise colors, or ColorIndex for the classic palette (e.g., ColorIndex = 6 is yellow). RGB is more flexible.
  • To color the entire row of the found cell, use foundCell.EntireRow.Interior.Color = RGB(255, 255, 0).
  • Store the search term in a cell or an InputBox so you can reuse the macro without editing code: searchTerm = InputBox("Enter value to find:").
  • If you need to search multiple sheets, loop through ThisWorkbook.Worksheets and run the find on each.
  • Use Application.ScreenUpdating = False at the start and True at the end for faster execution on large ranges.
  • Test on a copy of your workbook first. Coloring is non-destructive, but a misplaced loop can still slow Excel down.

Troubleshooting

  • Macro does nothing. Check that the code is in a standard module (Insert > Module), not a sheet module. Also confirm macros are enabled: File > Options > Trust Center > Trust Center Settings > Macro Settings. On Mac, check Excel > Preferences > Security.
  • Runtime error 91 "Object variable not set". You did not handle the Nothing case. Add the If Not foundCell Is Nothing check.
  • Find returns nothing but the value exists. The value may be a formula result, a number stored as text, or have leading/trailing spaces. Try LookIn:=xlFormulas or trim the search term. Also verify the range includes the cell.
  • Color does not appear. The cell may have a conditional formatting rule overriding the fill. Check Home > Conditional Formatting > Manage Rules. VBA-set colors are overridden by conditional formatting.
  • Macro is slow on large ranges. Limit the range (e.g., UsedRange instead of entire columns) and disable screen updating.
  • "Cannot run the macro" message. The workbook may be blocked. Right-click the file in File Explorer, choose Properties, and check Unblock if available. Also ensure the file is in a trusted location.

Frequently Asked Questions

Can I find and color a cell without VBA?

Yes. Use Home > Find & Select > Find, then apply a fill color manually, or use Conditional Formatting with a formula rule. VBA is only needed for automation or repeated tasks.

How do I color a cell based on a condition, not an exact match?

Use LookAt:=xlPart for partial matches, or loop through cells and test with InStr or Like for pattern matching.

Will the color stay if I change the cell value?

Yes, the fill is static. If you want dynamic coloring, use Conditional Formatting instead of VBA.

Can I search for a value and color the entire row?

Yes. After finding the cell, use foundCell.EntireRow.Interior.Color = RGB(255, 255, 0).

Why does my macro work on one sheet but not another?

The range or worksheet reference is likely hard-coded. Check that the sheet name in ThisWorkbook.Worksheets("Sheet1") matches the actual sheet, and that the range covers the data.

Is VBA available in Excel for the web?

No. VBA macros run only in the desktop versions of Excel for Windows and Mac. Excel for the web can open macro-enabled files but cannot run the macros.