How to Find and Color Cells in Excel with VBA
In this article
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) orFn + 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
- Open the VBA editor. Press
Alt + F11(Windows) orFn + 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. - Insert a new module. In the editor, go to Insert > Module. A blank code window appears. This is where your macro will live.
- 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 - Understand the key arguments.
LookIn:=xlValuessearches displayed values, not formulas.LookAt:=xlWholematches the entire cell; usexlPartto match substrings.MatchCase:=Falsemakes the search case-insensitive. - Run the macro. Press
F5while the cursor is inside the subroutine, or go back to Excel and use Developer > Macros, selectFindAndColor, and click Run. The first matching cell gets a yellow fill. - Color all matches (optional). To highlight every occurrence, wrap the search in a loop:
TheSub 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 SubfirstAddresscheck prevents an infinite loop. - Clear the color when needed. Use
foundCell.Interior.ColorIndex = xlNoneto remove the fill. To clear all highlights in a range, loop through cells or usesearchRange.Interior.ColorIndex = xlNone. - 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. IfFindreturns nothing and you immediately set.Interior.Color, you get a runtime error 91. Always testIf Not foundCell Is Nothingfirst. - 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 withws.Range. - Using
LookAt:=xlWholefor partial matches. Searching for "pen" withxlWholewill not find "Pending". Switch toxlPartfor substrings. - Assuming
Findsearches formulas. By default it searches values. If your cell contains a formula that returns "Pending",LookIn:=xlValuesworks; if you need to search the formula text itself, useLookIn:=xlFormulas. - Not resetting
Findsettings. Excel remembers the lastFindparameters. If a previous search usedMatchCase:=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, orColorIndexfor the classic palette (e.g.,ColorIndex = 6is yellow).RGBis 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
InputBoxso you can reuse the macro without editing code:searchTerm = InputBox("Enter value to find:"). - If you need to search multiple sheets, loop through
ThisWorkbook.Worksheetsand run the find on each. - Use
Application.ScreenUpdating = Falseat the start andTrueat 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
Nothingcase. Add theIf Not foundCell Is Nothingcheck. - 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:=xlFormulasor 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.,
UsedRangeinstead 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.