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:
- Click Debug on the error dialogue to locate the failing line in the Visual Basic Editor.
- 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") - Always prepend
Setwhen assigning instances ofRange,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:
- Examine procedures where
.Findis chained directly into properties such as.Rowor.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 - Always validate that the returned object reference is valid using
If Not [Variable] Is Nothing Thenbefore 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:
- Ensure that your code explicitly constructs the object using the
Newkeyword:' 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 - If an uninitialized object is referenced before the
Set ... = Newstatement 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:
- Ensure every
Withstatement has an unambiguous matchingEnd Withline. - Never use
GoTostatements to jump directly into the middle of aWithblock without evaluating the parent object instance.