Excel VBA Find Not Found: Handle Missing Matches
In this article
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
- 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.
- Declare your variables properly. Write
Dim foundCell As RangeandDim searchRange As Range. Declaring the result as aRangeis what lets you test it againstNothing. If you declare it as aVariantor skip the declaration underOption Explicit, you invite confusing bugs. - Set the range you are searching. For example
Set searchRange = ActiveSheet.Columns("A")orWorksheets("Data").Range("A2:A500"). Limiting the range is faster and avoids matching header rows you did not intend to search. - 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 writesearchRange.Find(...).Selectdirectly, a not-found result errors immediately. - Test for Nothing before doing anything else. Add:
If foundCell Is Nothing ThenMsgBox "Widget was not found."ElseMsgBox "Found in cell " & foundCell.AddressEnd If
This branch is the entire answer to "find not found." Everything else is style. - 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 DoorExit Sub. Pick one deliberately rather than letting the macro crash. - Handle the case where Find only appears to fail. If you know a value exists but Find returns
Nothing, the usual causes are aLookAt:=xlWholesetting with a partial value, aLookIn:=xlFormulasdefault 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. TryLookAt:=xlPart, then switchLookIntoxlValuesand test with a number. - Reuse the pattern inside loops. When looping through a list of codes, put the Find-and-test inside the loop and use
Do WhilewithFindNextonly 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. - 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.
- 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 = 1fails 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
"", soIf 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,LookInand other settings. If you specified them once, specify them again next time. - Declaring the result as
StringorVariant. You cannot compare those toNothingin the way Find requires. Keep it asRange.
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.Addressrather thanfoundCell.Rowin messages; the address includes the sheet name when needed and is easier to check. - Set
Application.ScreenUpdating = Falsearound long loops and restore it afterwards to speed up many Find calls. - If your data has trailing spaces, use
Trimon 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.