Runtime Error 91: Object variable or With block variable not set (The Set keyword trap)

VBA generates Runtime Error 91: Object variable or With block variable not set when code attempts to access a property or method of an object variable that currently points to Nothing. In almost all cases, this happens because an object was assigned without the mandatory Set keyword, or an object lookup method (such as Find) returned no result.

What Causes Runtime Error 91 in VBA?

Runtime Error 91 occurs when your code attempts to read or modify an object variable that has not been initialized with an active instance. This most commonly results from omitting the Set keyword during variable assignment, failing to handle an empty search result from Range.Find, or referencing an uninstantiated object inside a With block.

The Quick Fix: Add the Set Keyword

In VBA, standard primitive variables (numbers, strings, dates, booleans) use simple assignment (let syntax, where the keyword is omitted). Object variables (Worksheet, Range, Workbook, Collection, Dictionary) point to complex memory structures and must be assigned using Set.

' BROKEN: Omission of "Set" causes VBA to attempt value extraction or throw Error 91
Dim rng As Range
rng = Worksheets("Sheet1").Range("A1")

' FIXED: Use Set to bind the object reference
Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A1")
rng.Value = "Complete"

The Four Primary Scenarios That Trigger Error 91

1. The Missing Set Keyword Assignment Trap

When you write:

Dim ws As Worksheet
ws = ThisWorkbook.Worksheets("Data") ' Runtime Error 91

VBA interprets ws = ... as an attempt to assign the default property of the Worksheet object to ws. Because Worksheet has no default scalar property that matches, or because ws is typed as an object, execution halts immediately.

Always pair object variable definitions with Set:

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Data")

2. The Range.Find Null Return Trap

The .Find method returns a Range object if a match is found. If no match exists in the searched area, .Find returns Nothing.

If your code chains a property (such as .Row, .Address, or .Value) directly to the .Find method call, it will throw Error 91 whenever the search term is missing:

' DANGEROUS: Crashes with Error 91 if "Invoice Total" is not found
Dim foundRow As Long
foundRow = Cells.Find(What:="Invoice Total").Row

' SAFE: Split search and evaluation into two steps
Dim foundCell As Range
Set foundCell = Cells.Find(What:="Invoice Total", LookIn:=xlValues, LookAt:=xlWhole)

If Not foundCell Is Nothing Then
    foundRow = foundCell.Row
Else
    MsgBox "Invoice Total was not found in the worksheet.", vbExclamation
End If

Always assign .Find results to a temporary Range variable, then check If Not [variable] Is Nothing before accessing its members.

3. Early Reference Before Initialization

Declaring an object variable allocates space for the pointer, but the pointer defaults to Nothing until initialized:

Dim dict As Object
' Code here executes...
dict.Add "Key1", "Value1" ' FAILS with Error 91 because dict is Nothing

' Fix: Create or set the object instance first
Set dict = CreateObject("Scripting.Dictionary")
dict.Add "Key1", "Value1"

4. Premature Object Destruction

If your macro explicitly resets an object to Nothing (to free memory or close connections) and later attempts to reference that object within the same procedure or a downstream routine, Error 91 occurs:

Set myRange = Worksheets("Sheet1").Range("A1:B10")
' ... work performed ...
Set myRange = Nothing

' Later in the routine:
myRange.ClearContents ' FAILS: myRange is Nothing

For proper teardown practices that prevent premature deallocation, consult Excel Crashing on Close: How to properly clear Object variables from memory..


Diagnostic Comparison: Error 91 vs. Error 424

These two errors frequently occur in similar code contexts, but indicate different underlying problems:

Error CodeError MessageRoot Problem
Error 91Object variable or With block variable not setThe variable is properly declared as an object type, but points to Nothing at the moment of access.
Error 424Object RequiredAn expression or variable expected an object reference, but received a primitive data type (string, integer) or a non-existent object name.

If you encounter the latter, see Runtime Error 424: Object Required (Common in UserForm control references)..


Debugging Workflow for Error 91

When execution breaks on an Error 91 line:

  1. Check Object State in Watch Window: Add the variable to your Watch list (Debug > Add Watch). If its value displays as Nothing, trace backward through your routine to locate where it was supposed to receive its Set statement. Learn more in Using Watch Windows to track Object states..
  2. Force Variable Declaration: Add Option Explicit to the very top of your code module. This prevents typos in variable names from silently instantiating uninitialized Variant/Empty variables. Detailed setup is covered in “Variable not defined”: Why you must use Option Explicit..
  3. Audit Related Variable Errors: If variable types or assignment syntax remain inconsistent across multiple procedures, refer to the cluster guide at VBA Logic & Variable Errors: Troubleshooting Runtime Codes and Syntax Breaks.