Set Object Varialbe to Nothing - why?

Harry Flashman

Active Member
Joined
May 1, 2011
Messages
361
Excel VBA Variables. Using Variables in Excel VBA Macro Code
The above webpage offers the following advice about object variables:

Set rMyCell = Range("A1")

"So when we have finished using the Object Variable "rMyCell" it is a good idea to Set it back to it's default value of Nothing. eg: [...] This will mean Excel will not be reserving unnecessary memory."

Set rMyCell = Nothing

Could some please explain the issue here. Is the writer simply referring to memory used while the macro is running or is he also referring to Excel reserving memory even after the macro has finished running?

I occasionally see people set Object variable to nothing at the end of the code - but since the macro is finished what is the point of this?

Cheers
 
harry, when a variable is Set, it is allocated a place in memory. by setting to Nothing, this invokes a VBA command to release the memory for the OS again. it not only has no value, but is no longer part of the programs scope. it is good practice to remove objects from memory as they can be quite large and soon clog up working space.
hope that make sense.
 
Upvote 0
harry, when a variable is Set, it is allocated a place in memory. by setting to Nothing, this invokes a VBA command to release the memory for the OS again. it not only has no value, but is no longer part of the programs scope. it is good practice to remove objects from memory as they can be quite large and soon clog up working space.
hope that make sense.

Okay I am going to get into the habit of setting object variables to nothing when I have finished with them. Back to the second part of my quesition - if the object variable is not set to Nothing does it still take up memory once the routine has finished running? Or is that memory released automatically once the routine is finished?
 
Upvote 0
the variables are all wiped from memory when the subroutine finishes execution providing they are not set as public or global with a module. that is excels memory management at work.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top