How to Fix Excel VBA Run-Time Error 91 Object Variable Not Set

In Microsoft Excel Visual Basic for Applications (VBA), executing data processing automation routines or custom userform scripts may suddenly halt with the runtime message: Run-time error ’91’: Object variable or With block variable not set. This disruptive exception occurs when VBA instructions attempt to access properties or invoke methods on an object variable that currently points to Nothing in system memory. This issue commonly stems from omitting the mandatory Set keyword, failing to evaluate empty .Find results, or uninstantiated class references.

Include the Mandatory Set Keyword for Object Assignments

Unlike scalar primitive data types (such as Integer, Long, String, or Boolean) which are assigned with simple equality operators, all object variables in VBA require the explicit Set statement:

  1. Click Debug on the error dialogue to locate the failing line in the Visual Basic Editor.
  2. Inspect how object variables are assigned. For example, the following syntax triggers Error 91:
    ' Incorrect (causes Error 91):
    Dim rng As Range
    rng = Worksheets("Sheet1").Range("A1")
    
    ' Correct syntax:
    Dim rng As Range
    Set rng = Worksheets("Sheet1").Range("A1")
  3. Always prepend Set when assigning instances of Range, Worksheet, Workbook, Chart, or external COM objects.

Verify .Find Results Before Accessing Child Properties

The most widespread cause of Error 91 in enterprise Excel automation occurs when the Range .Find method fails to locate a search target in the spreadsheet and returns Nothing:

  1. Examine procedures where .Find is chained directly into properties such as .Row or .Address:
    ' Vulnerable syntax:
    targetRow = Cells.Find(What:="Total").Row
    
    ' Robust syntax:
    Dim foundCell As Range
    Set foundCell = Cells.Find(What:="Total", LookIn:=xlValues, LookAt:=xlWhole)
    
    If Not foundCell Is Nothing Then
        targetRow = foundCell.Row
    Else
        MsgBox "Search target not found in active sheet.", vbExclamation
    End If
  2. Always validate that the returned object reference is valid using If Not [Variable] Is Nothing Then before attempting to read its properties.

Instantiate Custom Classes and External Automation Libraries

If you create instances of custom class modules, Scripting Dictionaries, or ADODB database connections, declaring the variable alone does not allocate memory:

  1. Ensure that your code explicitly constructs the object using the New keyword:
    ' Declaration:
    Dim dict As Object
    ' Initialization (mandatory):
    Set dict = CreateObject("Scripting.Dictionary")
    
    ' Or with early binding:
    Dim dict As Scripting.Dictionary
    Set dict = New Scripting.Dictionary
  2. If an uninitialized object is referenced before the Set ... = New statement executes, Error 91 is triggered immediately.

Inspect With Block Termination and Scoping

Improperly nested or premature exits from With ... End With containers leave object references hanging in the call stack:

  1. Ensure every With statement has an unambiguous matching End With line.
  2. Never use GoTo statements to jump directly into the middle of a With block without evaluating the parent object instance.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.