Error 91 Flowchart vbahowto-com

Error 91: Object Variable or With Block Variable Not Set — Causes and Solutions

If you work with VBA (Visual Basic for Applications) in Excel, Access, Word, or other Microsoft Office programs, you’ve probably come across the dreaded Error 91 at least once:

Run-time error ‘91’: Object variable or With block variable not set

This error can be frustrating, especially if your code seems perfectly fine at first glance. But the good news is, once you understand what it means, it becomes one of the easier VBA errors to troubleshoot.


What Does Error 91 Mean?

In plain English, Error 91 occurs when your VBA code is trying to use an object variable that has not been assigned (or “set”) to an actual object yet.

Think of it like this: you’ve created a pointer to something (e.g., a worksheet, workbook, range, recordset, or form), but you never told VBA exactly what that pointer should point to. When VBA tries to access a property or method on that “nothingness,” it throws Error 91.


When Does Error 91 Happen?

Some common scenarios include:

  1. Forgetting to Use the Set Statement

    Dim ws As Worksheet

    ws.Name = "Data" ' ❌ Causes Error 91 because ws is Nothing

    Here, ws exists as a variable, but it’s not pointing to any actual worksheet yet

    Solution:

    Set ws = Sheets("Sheet1")

  2. Accessing Objects That Haven’t Been Initialized

    Dim rng As Range

    MsgBox rng.Address ' ❌ Causes Error 91 because rng has no range assigned

    Solution:

    Set rng = Range("A1:B5")

  3. Using a With Block on a Nothing Object

    Dim wb As Workbook

    With wb
    .Title = "Report" ' ❌ Causes Error 91 because wb is Nothing
    End With
  4. Solution:

    Set wb = ActiveWorkbook

  5. Recordset or Object from External Source Not Created
    This is common in Access when working with ADO/DAO:

    Dim rs As DAO.Recordset

    rs.MoveFirst ' ❌ Causes Error 91 if rs hasn’t been set via OpenRecordset
  6. Solution:

    Set rst = CurrentDb.OpenRecordset("TableName", dbOpenDynaset)


How to Fix Error 91

1. Always Use Set to Assign Objects

If you’re working with any object type (Worksheet, Range, Workbook, Recordset, etc.), you need to assign it before using it:

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Data")
ws.Name = "Updated Data"

The keyword Set tells VBA, “This variable points to an actual object in memory.”


2. Verify the Object Exists

If your code depends on something existing (like a sheet name), check before assigning:

Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Sheets("Data")
On Error GoTo 0
If ws Is Nothing Then
MsgBox “Sheet not found.”
Exit Sub
End If

This avoids assigning Nothing when the target doesn’t exist.


3. Initialize Objects Before Use

For objects that must be created first:

Dim dict As Object

Set dict = CreateObject("Scripting.Dictionary")
dict.Add "Key1", "Value1"

Without the Set line, dict would remain uninitialized and cause Error 91 when accessed.


4. Handle With Blocks Carefully

Before using a With block, make sure the object inside is valid:

Dim wb As Workbook
Set wb = ThisWorkbook
With wb
.Title = “Report”
End With

If wb was never set, the With block would trigger Error 91.


5. Debugging Tips

When Error 91 appears:

  • Use the Immediate Window (Ctrl+G) in the VBA editor to check:

    ? ws Is Nothing

    If it returns True, your object is uninitialized.

  • Use breakpoints (F9) to pause your code before the error and inspect variables.

  • Step through code with F8 to watch where the object fails to get set.


Example: Correcting an Error 91 in Excel VBA

Faulty Code

Sub RenameSheet()
Dim ws As Worksheet
ws.Name = "Summary"
End Sub

Problem: ws was never assigned to a sheet.

Fixed Code

Sub RenameSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Name = "Summary"
End Sub

Preventing Error 91 in Larger Projects

In bigger VBA projects, especially in Access or when automating multiple Office apps, Error 91 can be harder to track. Good habits to avoid it include:

  • Explicitly setting every object variable before use

  • Checking for Nothing before acting

  • Using descriptive variable names so you remember what’s been set

  • Structuring code so that all object assignments happen before operations

  • If you are trying to access a network folder, make sure the logged in user has folder network access.

Conclusion

Error 91 in VBA — Object variable or With block variable not set — is one of the most common runtime errors for new and experienced developers alike. The root cause is almost always the same: trying to use an object that hasn’t been assigned yet.

Once you understand the concept of setting objects and checking for Nothing, troubleshooting becomes much faster. Whether you’re working in Excel, Access, or another VBA host, the solution is to initialize your object variables before using them and to verify they point to a valid object. With these best practices, Error 91 will go from a mystery bug to a quick fix in your coding workflow.

Here is a little flowchart you can use:

Error 91 Flowchart vbahowto-com
Error 91 Flowchart vbahowto-com

Let me know if you have any specific questions.




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

How to Copy Data from One Worksheet to Another Using VBA in Excel

Learn how to copy data between worksheets in Excel using VBA step-by-step! This quick tutorial shows how to automate data movement, save time, and boost productivity. Watch: How to Copy Data from One Worksheet to Another Using VBA Why Automate Copying Data Between Worksheets? Copying and pasting data manually can take up valuable time and […]

How to Fix Run Time Error 1004 in Excel

If you work with Microsoft Excel frequently, chances ar ling for a solution. Fortunately, this error is well-documented, and there are several ways to resolve it. In this article, we’ll explore the causes of run time error 1004, practical steps to fix it, and preventive measures to reduce the chances of it happening again. What […]


Support these sponsors:
Tags: , , , ,
Previous Post
“Run-time error ‘1004’ Application-defined or object-defined error.” “Method Range of object _Global failed.” “Cannot insert the object.”
Excel related ms-excel-vba

How to Fix Run Time Error 1004 in Excel

Next Post

How To Escape Apostrophe In SQL Update Query

Leave a Reply

Your email address will not be published. Required fields are marked *