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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
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!
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,331
Latest member
smckenzie2016

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