Excel VBA Error 91: Why an Object Variable Is Not Set

VBA & macros · Updated November 19, 2026

Excel VBA Run-time error 91 means your code tried to use an object variable that does not currently refer to an object. Assign the object with Set before using it, and check whether methods such as Find returned Nothing before you access the result.

What Run-time error 91 means

The full message is usually “Run-time error ’91’: Object variable or With block variable not set.” In plain terms, VBA reached a line that expects an object—a workbook, worksheet, range, chart, collection, or other object—but the variable or expression points to no object. In VBA, that empty object reference is called Nothing.

For example, declaring a worksheet variable does not connect it to a worksheet. A declaration tells VBA what kind of reference the variable can hold; it does not select, open, or create the object. If code then tries to read a cell through that unassigned variable, error 91 is expected.

The error is about an object reference, not an ordinary value. A number variable holding zero or a string variable holding an empty string is not the same thing as an object variable holding Nothing. This distinction helps narrow the search: inspect the object immediately to the left of the failing property or method, and the expression used by any active With block.

How VBA object references work

Object variables are declared with a type such as Workbook, Worksheet, or Range. To point one at an existing Excel object, VBA uses the Set statement. For instance, setting a worksheet variable to ThisWorkbook.Worksheets("Data") gives the variable a reference to that worksheet. Without that assignment, the variable remains Nothing.

The word “object” can sound abstract, but common examples are practical: a workbook is an object, each sheet is an object, and a cell or group of cells is a range object. A procedure can also receive an object as an argument or get one back from a method. In each case, the reference must be valid when the code uses it. An object can also be deliberately cleared with Set variableName = Nothing; accessing it afterward can produce error 91.

A With block is shorthand for repeatedly using one object. For example, inside With ws, a line starting with a dot such as .Range("A1") means ws.Range("A1"). If ws is Nothing, the block cannot perform those operations. The same underlying problem applies whether the missing reference is named directly or supplied to With.

Find the line and identify the missing object

When the error dialog appears, choose Debug. The Visual Basic Editor highlights the statement where VBA stopped. In Excel for Microsoft 365 on Windows, open the editor with Alt+F11; on a Mac, the shortcut is commonly Option+F11, though some keyboards require Fn as well. You can also open the editor from the Developer tab. Excel for the web does not run VBA macros; use the desktop application to debug them.

Read the highlighted statement from the object outward. In ws.Range("A1").Value = 5, check ws first. In foundCell.Offset(0, 1).Value, check foundCell. If the highlighted statement begins with a dot, inspect the object named in the nearest enclosing With statement. The visible line may be long, but the missing reference is often one specific variable or returned value.

Use the Locals window or hover over a variable in break mode to inspect its value. The Locals window is available from View in the Visual Basic Editor. The Immediate window, opened with Ctrl+G on Windows, can also help inspect an expression while execution is paused. On Mac, use the corresponding Visual Basic Editor menu because keyboard shortcuts can vary. If the object variable displays Nothing, trace backward to find where it should have been assigned.

Common causes and what to check

  • A variable was declared but never assigned. A line such as Dim ws As Worksheet only declares the variable. Look for a missing or skipped Set ws = ... statement before the first use. This often happens after code is rearranged or an assignment is placed inside a conditional branch that did not run.
  • A method returned no object. The Range.Find method returns Nothing when it does not find a match. Code that immediately reads .Row or .Value from that result can then fail. Check the return value before using it.
  • A variable was cleared or overwritten. Search for every assignment to the object variable, including Set variableName = Nothing. A loop or error-handling branch might clear it, or a later assignment may replace a valid reference with an empty one.
  • A With block uses an unassigned variable. If the block says With targetRange, confirm targetRange refers to a range before the block begins. A leading dot inside the block relies on that reference.
  • An object-returning function did not provide a usable result. A custom function or automation call may return Nothing under some conditions. Check its documented behavior and the returned reference before calling properties or methods on it.

Not every failed lookup causes error 91. For example, requesting a worksheet by a name that does not exist commonly raises a subscript-out-of-range error instead. Error 91 specifically points to using an object reference that is not set; the exact error depends on which operation failed.

Check object results before using them

When a method can return no object, test for Nothing before accessing its properties. The following example looks for an exact match in column A of the Data sheet. It assumes that the sheet exists; if the sheet name is wrong, the worksheet lookup itself needs separate error handling.

Save a copy of the workbook before running a macro. VBA changes cannot be undone with Ctrl+Z, and a macro can make changes throughout a workbook.

Sub FindRecord()
    Dim ws As Worksheet
    Dim foundCell As Range

    ' Point the worksheet variable at the existing sheet.
    Set ws = ThisWorkbook.Worksheets("Data")

    ' Find an exact match in column A.
    Set foundCell = ws.Columns("A").Find( _
        What:="ABC", LookIn:=xlValues, LookAt:=xlWhole)

    If foundCell Is Nothing Then
        MsgBox "No matching record was found."
    Else
        MsgBox "Match found on row " & foundCell.Row
    End If
End Sub

The important safeguard is the If foundCell Is Nothing Then test. Only the Else branch reads foundCell.Row, so that property is used only when a match exists. The line breaks in the Find call use VBA’s line-continuation character, an underscore preceded by a space. The Find arguments specify an exact match and values; stating these options also avoids relying on some search settings remembered from Excel’s Find dialog.

Workbook and worksheet references need context

Excel can have several workbooks open, so qualify references deliberately. ThisWorkbook means the workbook containing the VBA code. ActiveWorkbook means the workbook currently active in Excel, which might be a different file. If a macro is stored in a personal macro workbook or add-in, those two references can point to different workbooks. A worksheet lookup should therefore use the workbook the code intends to modify.

Likewise, ActiveSheet depends on what the user has selected, while ThisWorkbook.Worksheets("Data") explicitly names a sheet in the macro’s workbook. Explicit references reduce accidental dependence on selection and make it easier to see what each object variable should represent. They do not, however, eliminate every possible error: the named workbook and worksheet must still exist, and the code must handle any lookup failure appropriate to its task.

Be careful when setting a range. A range reference should be qualified through its worksheet or workbook when context matters. For example, a bare Range("A1") refers to the active sheet in many contexts. If the code later changes the active sheet, an unqualified reference can target somewhere unexpected. That may not itself cause error 91, but it can conceal the real problem by making the wrong object look plausible.

A practical debugging and prevention routine

  1. Click Debug and note the exact highlighted line. Do not assume the first object mentioned in the procedure is the problem; focus on the statement that failed.
  2. List the object references used on that line. Check variables before properties and methods, and inspect the active With expression if the line begins with a dot.
  3. Inspect the value in break mode. Confirm whether it is Nothing, then trace its assignment and any branches that might skip or clear that assignment.
  4. Check conditional paths and lookup results. A variable may be assigned only when a condition is true, or a search may legitimately return no match. Handle those cases explicitly.
  5. Test the procedure with both expected and missing data. A macro should behave sensibly when a record, sheet, or optional object is absent, rather than assuming every lookup succeeds.

Adding Option Explicit at the top of a module is useful because it requires variables to be declared and catches many misspellings at compile time. It does not assign object variables or prevent error 91 by itself. Clear variable names, explicit workbook qualification, and checks for optional results make the source of a missing reference easier to spot.

Frequently Asked Questions

How do I fix Object variable or With block variable not set in VBA?
Use Debug to find the failing statement, identify the object variable or With expression used there, and make sure it has been assigned with Set. If it comes from a search or other optional result, test whether it is Nothing before accessing it.

Does Dim create a worksheet or range object?
No. Dim ws As Worksheet declares a variable that can hold a worksheet reference, but it does not create or select a worksheet. Assign an existing worksheet with Set ws = ThisWorkbook.Worksheets("Data"), or use an appropriate object-creation method for objects that can be created that way.

Why does Range.Find cause error 91?
Find returns Nothing when it finds no matching cell. The error occurs if code then reads a property such as Row or Value from that empty result. Test the result with If foundCell Is Nothing Then before using it.

Is error 91 the same as Object required?
No. They are different VBA errors. Error 91 says an object variable or With variable has not been set. “Object required” generally means an operation expected an object but received something that is not a usable object; inspect the exact highlighted line and error message rather than treating them as interchangeable.

Can I run VBA macros in Excel for the web?
No. Excel for the web can open a workbook containing macros, but it cannot create, run, or edit VBA macros. Use Excel desktop for Windows or Mac to run VBA; the editor and some keyboard shortcuts differ by platform.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.