Why release variables

sugrue

New Member
Joined
Feb 7, 2008
Messages
25
I am looking at some code that Bill wrote in his "VBA and Macros For Excel 2007" book and it includes:

Code:
  Dim WSD As Worksheet
  Dim WSM As Worksheet
  Dim WB As Wookbook
'
' other code that uses these variables
'
' Release Variables
  Set WB = Nothing
  Set WSD = Nothing
  Set WSM = Nothing
End Sub
What is the benefit of releasing the variables? Since this is right before the End Sub, won't the variables be released when the code ends? Since I am new to VBA, I want to develop good habits - should all variables be set to Nothing before ending the code?

Thanks,
Jack
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,820
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Generally speaking, VBA is now pretty good at memory management, so you don't really need to do that at the end of a routine (though you may like to explicitly release a variable when you are done with it just to free up its resources). It does no harm to explicitly clear the variables, though these days you probably won't get involved in a heated argument whichever side you choose!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,671
Messages
5,838,691
Members
430,564
Latest member
Raeyven

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
Top