Runtime Error 91 in Excel VBA: what it means and how to fix it

Error 91 — “Object variable or With block variable not set” — means your code tried to use an object variable that currently points at nothing. The variable exists; the object doesn't. This is one of the most honest errors VBA gives you: find which variable is empty, work out why, and the fix follows. No downloads, no repair tools.

What it means

In VBA, object variables (a Worksheet, a Range, a workbook, any class object) don't hold the object itself — they hold a reference to it. Until you assign that reference with the Set keyword, the variable's value is the special state Nothing. The moment code tries to read a property or call a method on a variable that is Nothing — ws.Name, rng.Value, anything — VBA stops with error 91. The same thing happens inside a With block whose object is Nothing.

First: find which variable is Nothing

  1. When the error dialog appears, click Debug. Excel highlights the line that failed. The object variable on that line is the empty one — but if the line uses several, test each.
  2. Press Ctrl+G to open the Immediate window and ask directly: type ? ws Is Nothing (using your variable's name) and press Enter. True confirms it — that variable was never successfully assigned, or was set back to Nothing.
  3. Now scroll up and find where that variable should have been assigned. Every cause below is a different way that assignment either never happened, quietly failed, or was undone.

The causes, most common first — and their fixes

Cause 1The Set is missing.

The classic. Object variables must be assigned with Set. Leave it out and the assignment either fails or assigns the object's default value instead of the object — and the variable stays Nothing.

Dim ws As Worksheet
ws = ThisWorkbook.Worksheets("Report")   ' wrong — no Set
ws.Range("A1").Value = 5                 ' error 91 here

Fix: add the keyword:

Set ws = ThisWorkbook.Worksheets("Report")

Rule of thumb: if the thing on the right is an object — a sheet, a range, a workbook, a dictionary, anything you created with New — the assignment needs Set.

Cause 2A lookup returned Nothing — most often Range.Find.

Find does not raise an error when the value isn't there; it returns Nothing, by design. Code that uses the result without checking it works perfectly right up until the first search that finds nothing — then error 91, usually reported by a user whose data changed.

Dim found As Range
Set found = ws.Range("A:A").Find("Invoice 1042")
found.Offset(0, 1).Value = "Paid"   ' error 91 when the invoice isn't in column A

Fix: test the result before using it, every time:

If Not found Is Nothing Then
    found.Offset(0, 1).Value = "Paid"
Else
    MsgBox "Invoice 1042 not found"
End If

The same pattern applies to anything that can hand back Nothing: dictionary lookups you've built yourself, functions that return an object, and Collection wrappers.

Cause 3The variable was set — then set to Nothing, or fell out of scope.

A well-meaning Set ws = Nothing at the end of one routine, a shared variable cleared by another macro, or the assignment living in a different procedure than the use: a variable declared inside Sub A does not exist in Sub B. When execution reaches the use, the (new, empty) variable is Nothing.

Fix: trace the variable's whole life: where it's declared, where it's Set, anywhere it's Set to Nothing, and whether the code path that errors actually passes through the assignment. If two routines need the same object, pass it as a parameter instead of relying on a shared variable being filled in first.

Cause 4The With block's object is Nothing.

The error message names this case directly: With someObject where someObject was never assigned raises 91 on the first line inside the block — which is misleading, because the lines inside look innocent. They are; the With line itself is the problem, for one of the reasons above.

Fix: check the object on the With line in the Immediate window (? someObject Is Nothing) and fix its assignment first. Don't debug the block's contents — they never got a chance to run.

Cause 5An object from another application or library failed to be created.

Code that creates objects from outside Excel — CreateObject("Scripting.FileSystemObject"), an Outlook or Word instance, a database connection object — can end up with Nothing if the creation failed, the library isn't available, or the reference broke. The 91 then appears at the first use, not at the creation line, which makes it look like Cause 1.

Fix: test immediately after creating: If fso Is Nothing Then handle it there, where the real problem is visible. In the VBA editor, also check Tools → References for any entry marked MISSING — a broken reference can stop object creation cold.

Do not download anything for this error. Error 91 lives in your code, not in Windows. “Runtime repair” downloads, registry cleaners, and DLL-fixer sites cannot assign a variable for you — they can only install things you don't need. The fix is one of the five causes above, and it's already on your machine.

If it still happens

Related errors

Runtime Error 424 · Runtime Error 1004 · Runtime Error 438