In VBA, Is Not Nothing is a comparison that checks whether an object variable currently references a valid object, and it is written as If Not myObject Is Nothing Then because VBA does not support a direct Is Not operator. This test is essential for avoiding runtime error 91 (Object variable or With block variable not set) before you use an object variable.
Why the Phrase Confuses Beginners
Many VBA learners expect to write If myObject Is Not Nothing Then because that matches English and other languages. VBA, however, treats Is as a comparison operator that only works with the Nothing keyword, and it does not accept Not between the object and Is. The correct syntax places Not before the entire expression: If Not myObject Is Nothing Then. This reads as “if it is not true that the object is nothing,” which is logically equivalent to “if the object exists.”
What Is Nothing in VBA?
Nothing is a special value that means an object variable does not reference any object. When you declare an object variable without assigning it, it defaults to Nothing. You also set a variable to Nothing to release its reference, which is common in cleanup code. Unlike a string or number, Nothing is not an empty value; it is a state of having no object reference at all.
For example, this code creates an object and then clears it:
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
MsgBox Not ws Is Nothing ' True
Set ws = Nothing
MsgBox Not ws Is Nothing ' False
How to Check If an Object Is Not Nothing
The standard pattern is:
If Not myObject Is Nothing Then
' Safe to use myObject
Else
' Handle the missing object
End If
You can also use the inverse check with If myObject Is Nothing Then to test for a missing object. Both are valid, but the Not ... Is Nothing form is the idiomatic way to test existence.
Common places to use this check:
- Before calling a method or setting a property on a variable that might not have been assigned.
- After using
FindorRange.Find, which returnsNothingif no match is found. - When iterating through collections that may contain empty slots.
- Before releasing object references in error-handling cleanup.
Common Mistakes and Misconceptions
One frequent error is writing If Not myObject = Nothing Then. The = operator does not work with objects in this context; you must use Is. Another mistake is forgetting the Set keyword when assigning an object. Without Set, VBA tries to assign a default property or raises an error, leaving the variable still Nothing.
A subtle bug occurs when you check an object that is Nothing but you call a property on it inside the same condition, like If Not myObject Is Nothing And myObject.Name <> "" Then. VBA evaluates the second part even if the first is false, causing error 91. Use nested If statements or the AndAlso-like short-circuit pattern by splitting the checks:
If Not myObject Is Nothing Then
If myObject.Name <> "" Then
' safe
End If
End If
Practical Example: Using Find
The Range.Find method returns a Range object or Nothing if no match is found. Here is a typical usage:
Dim rngFound As Range
Set rngFound = Range("A1:A100").Find(What:="target")
If Not rngFound Is Nothing Then
MsgBox "Found at " & rngFound.Address
Else
MsgBox "Not found"
End If
Without the check, calling rngFound.Address would raise error 91. Always test the result of Find before using it.
How to Avoid Error 91 (Object Required)
Error 91 appears when you try to use an object variable that is Nothing. To prevent it:
- Always initialize object variables with
Setbefore use. - Check for
Nothingafter operations that may fail, likeFind,Open, orGetObject. - Use
If Not obj Is Nothing Thenbefore accessing members. - In error handlers, set objects to
Nothingto avoid stale references.
For example, when opening a workbook that may not exist:
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks.Open("C:\missing.xlsx")
On Error GoTo 0
If Not wb Is Nothing Then
MsgBox wb.Name
Else
MsgBox "File not found"
End If
Comparing Is Nothing vs = Nothing
For objects, Is compares reference identity, while = compares values. Most objects do not have a default property that works with =, so using = with an object either causes an error or compares the wrong thing. The only safe way to test for Nothing is Is Nothing or Not ... Is Nothing. For value types like strings or numbers, you use = or <>, but never Is.
Frequently Asked Questions
Can I write “Is Not Nothing” without the Not before Is?
No. VBA syntax requires Not before the object expression: If Not obj Is Nothing Then. Writing If obj Is Not Nothing causes a compile error. This is a common VBA quirk that differs from languages like C# or Python.
What is the difference between Nothing and Empty?
Nothing is for object variables only. Empty is a value that an uninitialized Variant holds. A Variant can also hold Nothing if you assign an object reference to it and then set that object to Nothing. Use Is Nothing for objects and IsEmpty for Variants that might be uninitialized.
Why does my If Not obj Is Nothing still give error 91?
This usually happens because the condition itself is fine, but inside the block you call a method that returns Nothing again, or you used a property that triggers an implicit object dereference. Also, if you use And in the same condition, VBA evaluates both sides, so a Nothing object on the right side causes the error. Split the checks or use nested Ifs.
How do I check if a worksheet exists using Is Nothing?
You can loop through ThisWorkbook.Sheets and compare names, or use a helper function. A common pattern:
Dim ws As Worksheet
Set ws = Nothing
For Each s In ThisWorkbook.Sheets
If s.Name = "Data" Then Set ws = s
Next
If Not ws Is Nothing Then
MsgBox "Sheet exists"
Else
MsgBox "Sheet not found"
End If
Is there a shortcut to check if an object exists without If?
You can use the IIf function, but it evaluates both true and false parts, so it is not safe if the false part uses the object. A safer approach is a small helper function that returns a Boolean. For example:
Function Exists(obj As Object) As Boolean
Exists = Not obj Is Nothing
End Function
Then call If Exists(myObj) Then. This is cleaner but still requires the object to be passed by reference.
Related
- Formula for Concatenate in Excel & Google Sheets
- Software Entry Data Backup: How It Works & Best Practices
- 100 Computer Shortcut Keys: Windows & Mac Guide
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Sort Data: Methods, Examples, and Common FixesIn Excel VBA, sort data with the worksheet’s Sort object: define the range, add one or more sort…
- VBA Delete Current Row: Safe Methods and ExamplesTo delete the current row in Excel with VBA, delete the entire worksheet row that contains the active…
- VBA ArrayList: How to Create and Use It in Excel VBAIn VBA, an ArrayList is a resizable, zero-based list that you can create on Windows with CreateObject("System.Collections.ArrayList"). It…
- VBA Functions: What They Are and How to Use ThemA function in VBA is a named procedure that performs a task and returns a value. You can…
- Automatically Add Comments to Excel Cells with VBAYes. In desktop Excel, VBA can automatically add a note to a cell with Range.AddComment; in current Microsoft…
- VBA Format Date String: Examples, Patterns, and FixesIn VBA, use Format or Format$ to turn a date value into a text string: for example, Format$(Date,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.