🚀The world's best VBA AI has evolved. ExcelMaster is now an autonomous Agent.Read more →
Back to Blog

VBA Nothing in Excel — Releasing References, Is Nothing vs = Nothing, and When It Matters

|

VBA Nothing in Excel — Releasing References, Is Nothing vs = Nothing, and When It Matters

TL;DRNothing is the empty-reference state: an object variable that points at no object. Test for it with If x Is Nothing — the Is operator, never a plain =, because objects cannot be compared with =. Set x = Nothing releases the reference. Every object variable starts as Nothing, and using one while it is still Nothing is the source of error 91, Object variable or With block variable not set.

Sub NothingBasics()
    Dim ws As Worksheet              ' starts as Nothing - points at no sheet
    Set ws = FindSheet("Data")       ' a function that may return Nothing

    If ws Is Nothing Then            ' Is, not = (a plain = will not compile here)
        MsgBox "Sheet not found"
        Exit Sub
    End If

    ws.Range("A1").Value = 1
    Set ws = Nothing                 ' release the reference
End Sub

Nothing is the "release" step of the object lifecycle: you make an object with New, point a variable at it with Set, and drop the reference with Nothing. It is also the starting state of every object variable — which is exactly why an un-Set object blows up.

What you'll learn

  • The mental model — Nothing is an empty pointer, a label attached to no object
  • Why you must test with If x Is Nothing, and never If x = Nothing
  • The most common real use — guarding a Find or lookup that returned Nothing
  • When Set x = Nothing actually matters, and when it is a harmless ritual
  • How Nothing differs from Empty, Null and "" (they are not interchangeable)

The mental model: an empty pointer

If an object variable is a label that points at a thing, then Nothing is that label pointing at no thing. It is not zero, not an empty string, not "blank" — those describe values. Nothing is the specific state of a reference that has no object on the other end.

Every object variable begins there. The moment you write Dim ws As Worksheet, ws is Nothing; it stays Nothing until a Set points it at a real sheet. That single fact explains error 91: use an object variable before you Set it, and you are dereferencing NothingObject variable or With block variable not set (see Set). Nothing is both where a reference is born and where it goes when you release it.

Test with Is Nothing, never = Nothing

This is the trap that catches everyone once. You cannot test a reference for emptiness with =:

If ws = Nothing Then      ' WRONG - will not compile / type error
If ws Is Nothing Then     ' RIGHT - Is compares identity, not value

= asks "are these two values equal?", and comparing that way needs a default value — which is why Range("A1") = 5 works but ws = Nothing does not. Is asks a different question: "do these two labels point at the same object (or at no object)?" Testing for Nothing, or checking whether two variables refer to the same object, is an identity question, so it uses Is. To test the opposite, wrap it: If Not ws Is Nothing Then.

The number-one real use: guard a Find that returned Nothing

The single most common place Nothing shows up in real macros is the result of a search. Range.Find returns Nothing when it finds no match — and reaching for .Row on that result is a straight error 91:

Dim hit As Range
Set hit = ws.Columns("A").Find(What:="Total", LookAt:=xlWhole)

If hit Is Nothing Then                ' the guard that prevents error 91
    MsgBox "No 'Total' row found"
    Exit Sub
End If

MsgBox "Found in row " & hit.Row      ' safe - we know hit points at a real cell

Any API that "might not find one" — Find, a Dictionary lookup wrapped in a helper, a function that returns an object — can hand you Nothing. The habit that saves you: whenever an assignment might yield Nothing, test Is Nothing before you use the result.

When Set x = Nothing actually matters

Here is the honest answer most tutorials skip: for an ordinary local object variable, Set x = Nothing at the end of a Sub is usually redundant. VBA automatically releases local object references when the variable goes out of scope as the procedure ends. Setting them to Nothing first is harmless, but it is ritual, not necessity.

It genuinely matters in three cases:

  • Circular references. If object A holds a reference to B and B holds one back to A, they keep each other alive and never get reclaimed, even after the Sub ends. Explicitly Set A.Partner = Nothing breaks the cycle so both can be freed.
  • Module-level, global or Static object variables. These outlive the procedure that filled them, so they hold their object — and any Excel resources it locks — until you release them or the workbook closes. Release them deliberately when you are done.
  • External application objects. An Outlook.Application or Word.Application you created stays running in the background until every reference to it is Nothing. Release promptly so the other program can close.

The rule: release deliberately where a reference is long-lived or shared; do not cargo-cult Set x = Nothing onto every local variable.

Nothing is not Empty, Null or the empty string

Four different "emptinesses" trip people up because English lumps them together. VBA keeps them strictly apart:

  • Nothing — an object reference that points at no object (If obj Is Nothing).
  • Empty — an un-initialized Variant that has never been assigned (If IsEmpty(v)).
  • Null — a Variant deliberately holding "no valid data", most often from a database field (If IsNull(v)).
  • "" — a String of length zero. A real string, just empty (If s = "").

They are not interchangeable, and each has its own test. Using Is Nothing on a Variant, or IsNull on an object, will not do what you expect. Nothing is the object one — reserve it for references.

How ExcelMaster helps

Nothing sits at both ends of an object's life, and every wrong move around it fails in a way that is easy to miss: a = Nothing that will not compile, a Find result used without a guard that throws 91, an external app left running because a reference never got released.

ExcelMaster writes the guard for you — If Not hit Is Nothing Then after any search — uses Is for every identity test, and releases the long-lived and external references that actually need it without littering Set x = Nothing across every local. You describe the outcome; it handles the lifecycle. You keep the workbook and the code.

Frequently asked questions

What does Nothing mean in VBA?

Nothing is the empty-reference state of an object variable — the variable points at no object. Every object variable starts as Nothing until a Set gives it one, and Set x = Nothing returns it to that state. It applies only to objects, not to values.

Why can't I use If x = Nothing?

Because = compares values, and an object reference has no value to compare — so If x = Nothing fails to compile or raises a type error. Use the identity operator instead: If x Is Nothing. To test the opposite, write If Not x Is Nothing.

Do I need to Set objects to Nothing at the end of a macro?

Usually not. VBA releases local object variables automatically when the procedure ends, so Set x = Nothing on an ordinary local is a harmless ritual. It does matter for circular references, for module-level or global object variables that outlive the procedure, and for external application objects such as Outlook.Application that should be released promptly.

How do I check whether a Find or lookup returned nothing?

Test the result with Is Nothing before you use it: Set hit = ws.Columns("A").Find("Total") then If hit Is Nothing Then ... Exit Sub. Range.Find returns Nothing when there is no match, and touching .Row on a Nothing result raises error 91, Object variable or With block variable not set.

What is the difference between Nothing, Empty, Null and the empty string?

Nothing is an object reference pointing at no object; Empty is an un-initialized Variant; Null is a Variant holding "no valid data", often from a database; "" is a zero-length String. They are distinct states with distinct tests (Is Nothing, IsEmpty, IsNull, = "") and are not interchangeable.

Tested in

Tested in: Excel 365 (Windows 11), VBA 7.1 — last verified 2026-09-19.

Related guides: VBA Set · VBA New · VBA Dim · VBA CreateObject · VBA With