Excel VBA Runtime Error 91: Causes and Fixes

VBA & macros · Updated November 19, 2026

Excel VBA runtime error 91 means your code tried to use an object variable that does not currently refer to an object. Find the line highlighted by the debugger, identify the object being used there, and make sure it has been assigned with Set before you read a property or call a method.

What runtime error 91 means

The full message is usually “Object variable or With block variable not set.” In VBA, an object variable is a reference to something such as a worksheet, workbook, range, chart, or Outlook item. Declaring the variable reserves a place for that reference; it does not automatically connect the variable to an actual Excel object.

For example, Dim ws As Worksheet declares a worksheet variable, but ws has no worksheet assigned yet. A line such as ws.Range("A1").Value = 1 can then raise error 91 because VBA has no worksheet through which to reach the range. The usual assignment is Set ws = ThisWorkbook.Worksheets("Data").

The error is about an unassigned or unavailable object reference, not a blank cell, a zero value, or a missing worksheet value. A missing worksheet name in a collection lookup commonly causes a different error, such as “Subscript out of range.” Error 91 often appears later, when code uses a variable that was never assigned or when a function returned no object.

How VBA object references work

VBA distinguishes object variables from ordinary values. A variable that holds a number, text, or date is assigned with the usual assignment statement. An object variable is assigned with Set, which tells VBA to store a reference to an existing object. For example, a Range reference can point to a cell, while a Worksheet reference can point to a sheet in a workbook.

Until an object variable receives a reference, it is Nothing. It can also become Nothing if code explicitly releases it with Set variable = Nothing. Calling a method or reading a property through a Nothing reference is the central situation behind error 91. An object returned by a function may also be Nothing when there is no matching object to return.

This is why the declaration alone is not enough. A variable can be correctly declared, compile without complaint, and still fail at run time because a particular code path did not assign it. The problem may depend on the selected sheet, a search result, or whether a user opened a workbook before starting the macro.

Common causes in Excel macros

  • A variable was declared but never assigned. The code declares a Worksheet or Range and then uses it without a successful Set statement.
  • An assignment only happens in some branches. An If condition or loop may skip the line that sets an object, while later code assumes it was set.
  • Find returned no match. Excel’s Range.Find method returns a Range object when it finds a match, but returns Nothing when it does not. Using the result without checking it can produce error 91.
  • A With block uses an unset variable. A block such as With ws needs ws to refer to a worksheet before the block’s properties or methods are used.
  • An object is expected from another procedure. A function or property may return an object, but return Nothing in a particular case, or the calling code may fail to assign the returned object.
  • A loop or event changes the state. A reference may be set on one pass through a loop but not the next, or an event procedure may run when an expected workbook or sheet is not active.

Not every object-related failure is error 91. For example, an invalid worksheet name in Worksheets("Data") usually raises error 9 before VBA ever uses the worksheet variable. Identify the actual error number and highlighted statement rather than assuming every failed object lookup has the same cause.

Find the exact line and inspect the object

When the error dialog appears, choose Debug to return to the Visual Basic Editor and highlight the statement that failed. That statement is the best starting point, but the underlying cause may be earlier: trace the object variable back to where the code was supposed to assign it.

  1. Read the highlighted line and list each object it uses. In ws.Range("A1").Value, for instance, check ws first.
  2. Search upward for the assignment to that variable. Confirm that it runs on the current path and that it uses Set.
  3. Step through the procedure with F8 in the Windows Visual Basic Editor. On some keyboards, use Fn+F8 if function keys control media or system features.
  4. Inspect variables in the Locals window or add a watch. The Immediate window can also help; in the Windows editor, open it with Ctrl+G. On Mac, use the editor’s Debug and View menus if the keyboard shortcut differs.
  5. Check assumptions about the workbook, sheet, search term, or user action. A line that works for one workbook state may fail when a sheet is missing or a search finds nothing.

On Windows, F9 toggles a breakpoint so execution can pause before the failing statement. On Mac, function-key behavior can depend on keyboard settings; use the Visual Basic Editor’s Debug menu for stepping and breakpoints if needed. A breakpoint helps you inspect the state before the error, rather than only after the failing line has stopped execution.

Fix an unassigned worksheet or range

For a worksheet that is known to exist, assign it explicitly before using it. The example below writes a status value to cell B2 on a sheet named Data in the workbook containing the macro. Before running a macro, save a copy of the workbook: VBA actions cannot be undone with Ctrl+Z.

Sub MarkDataSheet()
    Dim ws As Worksheet

    ' Assign the worksheet before using it.
    Set ws = ThisWorkbook.Worksheets("Data")
    ws.Range("B2").Value = "Checked"
End Sub

ThisWorkbook means the workbook that contains the VBA code. It is not necessarily the workbook currently active on screen. Use it when the macro should work with its own workbook; use ActiveWorkbook only when the currently active workbook is deliberately the target. A bad sheet name generally causes an error during the assignment, so verify the tab name and the intended workbook if the failure occurs on the Set line.

If the sheet may legitimately be absent, handle that case deliberately. A narrowly scoped error handler can allow the lookup to fail, after which the code can test whether the reference is Nothing. Keep error suppression around only the lookup; leaving On Error Resume Next active across a larger procedure can hide unrelated problems.

Sub CheckForDataSheet()
    Dim ws As Worksheet

    ' Suppress an error only while looking up the optional sheet.
    On Error Resume Next
    Set ws = ThisWorkbook.Worksheets("Data")
    On Error GoTo 0

    If ws Is Nothing Then
        MsgBox "The Data sheet was not found."
        Exit Sub
    End If

    ws.Range("B2").Value = "Checked"
End Sub

Handle Find results and With blocks safely

A frequent source of error 91 is using the result of Find as if a match were guaranteed. Test the returned Range before reading or changing it. The example searches column A on the Data sheet for an exact match to North and reports when there is no result. As with any macro, save a copy of the workbook before running it because Ctrl+Z does not reverse VBA changes.

Sub LocateNorth()
    Dim ws As Worksheet
    Dim hit As Range

    ' Assign the sheet, then search for an exact cell match.
    Set ws = ThisWorkbook.Worksheets("Data")
    Set hit = ws.Columns("A").Find( _
        What:="North", LookIn:=xlValues, LookAt:=xlWhole)

    If hit Is Nothing Then
        MsgBox "North was not found in column A."
    Else
        MsgBox "Found North in " & hit.Address(False, False)
    End If
End Sub

For a With block, make sure the expression after With refers to a real object before relying on members inside the block. If the expression comes from a search or optional lookup, test it first. A With block does not create the object or assign a variable; it simply makes it easier to refer repeatedly to an object that already exists.

Prevent the error and avoid misleading fixes

Use Option Explicit at the top of each module. It requires variables to be declared and catches misspelled variable names at compile time. It cannot guarantee that every object variable is assigned, but it removes a separate class of mistakes that can make object-reference bugs harder to find.

  • Assign object variables close to the point where they are used, and give them meaningful names such as sourceSheet or matchCell.
  • Check optional results, especially values returned by Find or by a procedure whose documented behavior allows Nothing.
  • Keep error handling narrow. Do not use broad error suppression as a substitute for checking whether an object exists.
  • Make workbook and worksheet choices explicit. Avoid relying on the active sheet when the macro needs a specific sheet.
  • Test alternate paths, including no search match, an unexpected active workbook, and a skipped conditional branch.

Adding Set variable = Nothing is not a general repair. It clears a reference; it does not create one. Likewise, adding another Dim statement will not fix a variable that is already declared but never assigned. The repair is to establish the intended reference, or to handle the valid case in which no object should be available.

Excel version and platform notes

The meaning of error 91 is the same in Excel for Microsoft 365 on Windows and Excel for Microsoft 365 on Mac because it is a VBA object-reference error. The Visual Basic Editor and keyboard behavior can differ, particularly for function keys, so use the editor’s menus when a Windows shortcut does not work on a Mac.

Excel for the web does not run VBA macros. Open the workbook in the desktop Excel app to run or debug VBA. Macros that depend on Windows-only components, such as certain COM automation or ActiveX controls, may need separate changes on Mac; that is distinct from error 91, though a platform-dependent object failing to initialize could leave later code with no usable reference.

Frequently Asked Questions

What does Object variable or With block variable not set mean?
It means VBA tried to use an object reference that is Nothing. Assign the object with Set before using it, or check whether an optional result is Nothing before accessing its properties or methods.

Does error 91 mean the worksheet name is wrong?
Not necessarily. A worksheet collection lookup using a nonexistent name commonly raises error 9, “Subscript out of range.” Error 91 more often occurs when code uses an object variable that was not assigned. Check which exact line is highlighted to distinguish the cases.

Why does my Find statement cause error 91?
Find returns Nothing when it cannot find a match. Assign the result to a Range variable with Set, then test If result Is Nothing Then before using its address, value, or other properties.

Can I fix runtime error 91 by adding Set?
Only if the missing assignment is the problem and the right object is available. Adding Set to an assignment is necessary when assigning an object reference, but it does not fix an invalid workbook or sheet lookup, a skipped branch, or a legitimate no-match result.

Can Excel for the web run VBA macros?
No. VBA macros run in the desktop Excel application, not Excel for the web. Open the workbook in desktop Excel to inspect the code and use the Visual Basic Editor to debug it.

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.