VBA Reference counting – Object destruction

calendar_today Asked Jul 9, 2012
thumb_up 7 upvotes
history Updated April 16, 2026

Question posted 2012 · +19 upvotes

Lately I’ve bumped into a question that made me pounder; it kept me busy and I couldn’t find a transparent explanation for it on the net.
It is related to the destruction of Excel objects (which I use all the time and never really questioned before).

Background leading to my question:
With regular objects, you can instantiate an object using the keywords SET and NEW. For example:

Set classInstance = New className

Whenever we instantiate this way, the object is created in the heap memory and the reference counter is increased by 1.
In case I don’t add more references, the following statement would bring the reference count back to zero:

Set classInstance = Nothing 

When the reference count goes to 0, the object is destroyed and cleared from memory and the “classInstance” points to .

What I’ve read:
When we use the “CREATEOBJECT” function, it returns a reference to a COM object.

Set oApp = CreateObject("Excel.Application")

Even though we could say:

Set oApp = nothing 

The objects’ reference count will go to 0, and oApp will not point to the object anymore.

My questions:
1) Why is it that this type of object requires to call the method .Quit before the object is actually being removed from memory?
The same goes when adding a reference to a workbook object (workbooks.add or workbook.open) which requires the .close method. Why can’t these objects be automatically destroyed when bringing the reference count to zero?
Which is the case when we say for example:

set oRange = nothing 

2) And is there a need to say:

oApp.Quit
set oApp = nothing 

Since the Application object is already cleared from memory when applying .Quit, there is no object to be released anymore.
The only reason I could come up with, why oApp would be set to Nothing after Quit, would be because it could be pointing to an unused memory location (on the heap) and could lead to confusion later if this memory would be re-assigned (although in VBA I find this hard to imagine). I was questioning myself if this conclusion is correct and I would like to receive confirmation for that from someone who knows the answer.
Please, tell me if I see this wrongly.

3) What they call in VBA “a reference to an object” (such as oApp in the code above), I see them as pointer variables in C. Would it be safe to use this statement or again, am I seeing this wrongly?

Generally is not hard to apply .Quit and set to nothing, but it would be nice to receive some accurate information on the topic. So that I know for 100% percent why I am doing it.

Accepted answer +7 upvotes

Good Question 🙂

Excel controls the creation of its objects. Likewise it also controls their destruction.

Setting oApp = Nothing just destroys the object reference. It doesn’t remove the Application. To destroy an Excel object, you have to use it’s .Quit method.

Whenever you do, Set x = Nothing, the reference(pointer) named x to its relevant object is removed. This doesn’t mean that the object itself will be removed from the memory. Whether the object will be removed from memory or not, depends on various factors.

  1. Whether there are more references pointing towards the same object. If there are, the object will not be removed. The reference count must be zero.
  2. The internal implementation of the destructor of that object.

The .Quit method is defined to graciously remove all the memory objects excel has allocated, and close itself.

It is similar to calling Close on a form in VB6. Take for example, a form in vb6.

Dim f As Form
Set f = Form1
f.Show

'
'~~> Rest of the code
'

Set f = Nothing

Will this destroy the form? 🙂

FOLLOWUP

How about question 2? Thanks – Kim Gysen 14 mins ago

enter image description here

It might not be exactly as shown here, and compiler optimizations may make things behave differently… but this is the basic concept that is at work.

Top excel-vba Q&A (6)

+7 upvotes ranks this answer #101 out of 136 excel-vba solutions on this site .