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:
-
Forgetting to Use the
Set
StatementHere,
ws
exists as a variable, but it’s not pointing to any actual worksheet yetSolution:
Set ws = Sheets("Sheet1")
-
Accessing Objects That Haven’t Been Initialized
Solution:
Set rng = Range("A1:B5")
-
Using a
With
Block on a Nothing Object -
Recordset or Object from External Source Not Created
This is common in Access when working with ADO/DAO:
Solution:
Set wb = ActiveWorkbook
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:
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:
This avoids assigning Nothing
when the target doesn’t exist.
3. Initialize Objects Before Use
For objects that must be created first:
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:
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: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
Problem: ws
was never assigned to a sheet.
Fixed Code
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:

Let me know if you have any specific questions.
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: