To find data in Excel with Visual Basic for Applications (VBA), use the Range.Find method. The shortest working pattern is: set a range variable to the search area, call Set found = rng.Find(What:="text", LookIn:=xlValues, LookAt:=xlWhole), then check If Not found Is Nothing Then before acting on the result. This guide walks through that pattern step by step, plus the pitfalls that make Find return nothing or loop forever.

What You'll Need

  • Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer; on Mac, Excel > Preferences > Ribbon & Toolbar).
  • A workbook with data to search — a column of names, product codes or dates works well for practice.
  • Basic familiarity with the VBA Editor (Alt+F11 on Windows, Fn+Option+F11 on many Macs) and how to insert a module.
  • Macro security set to allow macros in your own files (File > Options > Trust Center > Trust Center Settings > Macro Settings).

Step-by-Step

  1. Open the VBA Editor and insert a module. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). In the Project pane, right-click your workbook name, choose Insert > Module. A blank code window appears.
  2. Write a Sub that declares your variables. Type Sub FindExample(), then Dim rng As Range, Dim found As Range, and Dim searchTerm As String. Declaring types keeps the code readable and avoids Variant surprises.
  3. Set the search range. Use Set rng = ActiveSheet.UsedRange for the whole used area, or something narrower such as Set rng = ActiveSheet.Columns("B"). Narrow ranges are faster and less likely to match a stray header.
  4. Assign the search term. For a fixed example, searchTerm = "Widget". To prompt the user, use searchTerm = InputBox("Find what?"). If the box is cancelled, searchTerm is empty — check for that before searching.
  5. Call the Find method. Write Set found = rng.Find(What:=searchTerm, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False). LookIn:=xlValues searches displayed values rather than underlying formulas; LookAt:=xlWhole requires an exact cell match, while xlPart allows partial matches.
  6. Test the result before using it. Add If found Is Nothing Then … MsgBox "Not found" … Else … MsgBox "Found at " & found.Address … End If. Skipping this test is the number-one cause of run-time error 91.
  7. Act on the found cell. Inside the Else branch you can select it (found.Select), highlight it (found.Interior.Color = vbYellow), or read its value into another cell. Remember found is a Range, so use found.Value, not found alone.
  8. Find every match, not just the first. Find returns only one cell per call. To loop, store the first address, then call FindNext(found) repeatedly until the address repeats. A safe skeleton: firstAddress = found.Address, then Do … Set found = rng.FindNext(found) … Loop While Not found Is Nothing And found.Address <> firstAddress. Without the address check the loop runs forever.
  9. Run the macro. Press F5 inside the Sub, or close the editor and use Developer > Macros. If Excel asks which macro to run, pick FindExample.
  10. Save as a macro-enabled workbook. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Ordinary .xlsx files discard the code on save.

Common Mistakes

Not testing for Nothing. If no cell matches, Find returns Nothing. Any property access on Nothing throws error 91. Always guard with If Not found Is Nothing.

Forgetting the address check in FindNext loops. FindNext wraps around to the first match, so a loop without an address comparison never ends. Ctrl+Break stops it, but you lose unsaved work if you force-quit.

Searching formulas when you meant values. With LookIn:=xlFormulas, a cell showing 1,200 may store =SUM(A1:A3) and will not match "1200". Switch to xlValues for what users see.

Assuming Find remembers your settings. Find keeps the last-used options from the Find dialog or previous calls. If a search behaves oddly, pass every argument explicitly — What, LookIn, LookAt, MatchCase, SearchOrder, SearchDirection — rather than relying on defaults.

Using ActiveSheet when the sheet may not be active. Reference sheets by name (Worksheets("Data")) so the macro works regardless of which tab is visible.

Tips

Use LookAt:=xlPart for fuzzy matching — searching "widget" then finds "Blue Widget XL". Use xlWhole when partial matches would produce false positives, such as product codes.

Set MatchCase:=False unless case matters; the default behaviour can surprise you if a previous search left it True.

For date searches, pass a real date value (What:=DateSerial(2024,1,15)) rather than a formatted string, and set LookIn:=xlFormulas if dates are stored as serial numbers.

Turn off Application.ScreenUpdating at the start of long loops and back on at the end; it makes bulk highlighting visibly faster.

Write a small helper function that accepts a range and a term and returns the found Range (or Nothing). Reusing it keeps your main procedures short and consistent.

Troubleshooting

Error 91 "Object variable or With block variable not set". You used found before confirming it is not Nothing. Add the guard from step 6.

The macro finds nothing although the text is visible. Check three things in order: LookIn is xlValues; the range actually covers the column holding the data; and the cell does not contain a leading apostrophe or trailing space. Trim inputs with Trim(searchTerm) and consider LookAt:=xlPart.

The loop never stops. Press Ctrl+Break (Windows) or Cmd+Period (Mac) to interrupt. Then add the firstAddress comparison shown in step 8.

Nothing happens when you press F5. The cursor may be outside the Sub, or the module may be in a different workbook. Click inside the procedure first, and confirm the correct project is highlighted in the Project pane.

Macros are disabled. Check the yellow security bar and your Trust Center macro settings. If the file came from elsewhere, you may need to unblock it in file properties before macros run.

Find works on one sheet but not another. The other sheet may be protected or filtered. Unprotect it or clear filters, since hidden rows are skipped by Find when a filter is active.

Frequently Asked Questions

Can Find search multiple worksheets at once?

Not directly. Range.Find works on one range on one sheet. Loop through Worksheets and call Find on each sheet's used range.

Does Find work on hidden rows or columns?

It skips rows hidden by an AutoFilter, but it does find cells in manually hidden rows. Clear filters first if results seem missing.

How do I search for a wildcard character like an asterisk?

Prefix it with a tilde: What:="~*" finds a literal asterisk. The same applies to question marks (~?).

Why does Find return the wrong cell after I change the range?

Find retains settings from prior calls. Pass all arguments explicitly each time, and reset SearchDirection to xlNext if you previously used xlPrevious.

Can I use Find to locate a cell by formatting only?

Yes — leave What empty and supply SearchFormat:=True after setting Application.FindFormat. This is advanced; test on a copy of the workbook first.