Smart Systems for Home

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

Excel VBA Find Not Found: Handle Missing Matches

· 6 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

When Excel VBA's Find method fails to locate what you asked for, it does not throw an error — it returns Nothing. That is the whole trick: you check If rng Is Nothing Then before touching rng.Row, rng.Value or anything else. Skip that check and the next line raises runtime error 91, "Object variable or With block variable not set." The fix is a small, reliable pattern you write once and reuse: attempt the find, test for Nothing, then decide what to do — skip, message the user, or write a default value.

What You'll Need

  • A copy of Excel with the Developer tab enabled, or just Alt+F11 to open the Visual Basic Editor directly.
  • A worksheet with some data to search — even a few rows in column A is enough to practise.
  • Basic comfort with macros: inserting a module, writing a Sub, and pressing F5 to run it.
  • A saved copy of the workbook if you plan to let a macro write back into cells. Macros cannot be undone with Ctrl+Z.

Step-by-Step

  1. Open the editor and add a module. Press Alt+F11, then choose Insert > Module. All the code below goes in that module. Menu wording and the ribbon layout can vary slightly between Excel versions and between Windows and Mac.
  2. Declare your variables properly. Write Dim foundCell As Range and Dim searchRange As Range. Declaring the result as a Range is what lets you test it against Nothing. If you declare it as a Variant or skip the declaration under Option Explicit, you invite confusing bugs.
  3. Set the range you are searching. For example Set searchRange = ActiveSheet.Columns("A") or Worksheets("Data").Range("A2:A500"). Limiting the range is faster and avoids matching header rows you did not intend to search.
  4. Call Find once and store the result. A typical line is:
    Set foundCell = searchRange.Find(What:="Widget", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
    Always assign the result to a variable. If you write searchRange.Find(...).Select directly, a not-found result errors immediately.
  5. Test for Nothing before doing anything else. Add:
    If foundCell Is Nothing Then
        MsgBox "Widget was not found."
    Else
        MsgBox "Found in cell " & foundCell.Address
    End If
    This branch is the entire answer to "find not found." Everything else is style.
  6. Decide what "not found" means for your task. Three common behaviours: report it to the user with a message box; skip that item silently and move to the next in a loop; or treat it as a clean exit from a loop using Exit Do or Exit Sub. Pick one deliberately rather than letting the macro crash.
  7. Handle the case where Find only appears to fail. If you know a value exists but Find returns Nothing, the usual causes are a LookAt:=xlWhole setting with a partial value, a LookIn:=xlFormulas default that misses displayed values, a value stored as text rather than a number, or (on a filtered or hidden sheet) rows that Find cannot see in the expected way. Try LookAt:=xlPart, then switch LookIn to xlValues and test with a number.
  8. Reuse the pattern inside loops. When looping through a list of codes, put the Find-and-test inside the loop and use Do While with FindNext only when you need every match. For a single match per item, one Find plus one Nothing test is enough. Remember Find remembers its last arguments, so restate them each call or reset the range.
  9. Test with a value you know is missing. Run the macro once with a real value and once with something like "XYZNotThere". Confirm you get the message box rather than error 91. That single test catches nearly every practical find-not-found bug.
  10. Save the workbook as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm), otherwise your code is discarded when the file closes.

Common Mistakes

  • Chaining methods onto Find. Range("A:A").Find("x").Offset(0, 1).Value = 1 fails with error 91 when nothing matches. Split it into a Find, a test, then the action.
  • Checking for an empty string instead of Nothing. A Range object is never "", so If foundCell = "" is not a valid test and produces a type mismatch.
  • Forgetting that Find is case-insensitive by default. A search for "apple" also matches "Apple" unless you set MatchCase:=True. Users often report a wrong match as a not-found bug.
  • Assuming Find searches the whole sheet. It searches only the range you give it. Searching Range("B2:B10") will never find something in column C.
  • Reusing arguments from the last call. Find retains the previous call's What, After, LookIn and other settings. If you specified them once, specify them again next time.
  • Declaring the result as String or Variant. You cannot compare those to Nothing in the way Find requires. Keep it as Range.

Tips

  • Wrap your search in a small helper function, for example one that accepts a range and a value and returns the found cell or Nothing. That way the Nothing check exists in one place.
  • Use foundCell.Address rather than foundCell.Row in messages; the address includes the sheet name when needed and is easier to check.
  • Set Application.ScreenUpdating = False around long loops and restore it afterwards to speed up many Find calls.
  • If your data has trailing spaces, use Trim on your search term and consider cleaning the column first. Find is exact about spaces.
  • Test against a small range before running on thousands of rows; a not-found test that works on ten rows behaves the same on ten thousand.

Troubleshooting

If the macro still stops with error 91, look at the line number: it is almost always a line that uses the Find result without a Nothing test. Add the test, rerun.

If Find returns Nothing for a value you can clearly see, check for stray characters. A leading apostrophe, a non-breaking space pasted in from a web page, or a number formatted as text all cause misses. Test by typing the value fresh into the search box of the Find & Replace dialog (Ctrl+F) — if the dialog also finds nothing, the problem is the data, not the code.

If the macro matches the wrong cell, your After argument or starting range is probably pointing past the first match; Find starts after the supplied cell and wraps around. Omit After unless you need it.

If the macro runs but does nothing, check whether the worksheet is protected or the range is filtered. Also confirm you are searching the sheet you think you are — ActiveSheet may not be the one in front when the macro starts.

Frequently Asked Questions

Does Find throw an error when nothing matches?

No. It returns Nothing. The error only appears when you use that Nothing result, such as calling .Value on it.

What does error 91 mean here?

"Object variable or With block variable not set" — you used the Find result before checking it. Add If foundCell Is Nothing Then.

Can I use Find across several sheets?

Not with one call. Loop through the worksheets collection and run Find on each sheet's range, testing for Nothing each time.

Why does Find miss numbers stored as text?

LookIn:=xlValues matches what is displayed; a text-formatted number is a string. Search for the text form or convert the column to numbers first.

Does Find work on hidden rows?

It can return hidden cells depending on your range and settings, which surprises people. If you need visible cells only, build a loop and check EntireRow.Hidden.

Is there a way to find all matches at once?

No single call returns every match. Use FindNext in a loop, remembering the first found address and stopping when you return to it.