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
 

Some videos you may like

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,065
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,371
Messages
5,595,784
Members
414,020
Latest member
Meghdad

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