VBA: Setting objects to Nothing

20 Ton Squirrel

New Member
Joined
Aug 18, 2011
Messages
11
Hello all! This is a "best practice" sort of question for you fellow programmers out there.

Is it necessary to always set object variables to Nothing at the end of a procedure?

In a procedure where any sort of object variable is declared, I always make a habit of setting the object to nothing at the end of the procedure. An example:

Code:
Public Sub sampleProcedure()
  Dim wks As Worksheet
  Dim rng As Range
  Dim dct As Scripting.Dictionary

    Set wks = ThisWorkbook.Worksheets("Sheet1")
    Set rng = wks.Range("A1")
    set dct = New Scripting.Dictionary

    '«BRILLIANT AUTOMATION PROGRAMMING HERE»

HANDLER_EXIT:
    Set wks = Nothing
    Set rng = Nothing
    set dct = Nothing

    Exit Sub

HANDLER_ERROR_SOMETHING1:
    '«BRILLIANT ERROR HANDLING HERE»

HANDLER_ERROR_SOMETHING2:
    '«BRILLIANT ERROR HANDLING HERE»

End Sub

I do this to make certain that the memory is being released for that particular variable. Doesn't matter if it is a range, worksheet, collection, or some wild class object... I always set it to Nothing.

Am I being OCD or is this a valid concern?
 
Generally not necessary. :) If you look at my code you will see I do it anyway with most non-Excel objects, but not because I need to. I think the only exception I know of is DAO objects (and even that may be out of date). VBA is basically very good about "garbage collection" and, well, let's face it, memory is cheap and plentiful these days.

It was the quirkiness in the DAO library that inflicted this habit on my psyche! Uggghhhhh… scarred for life! If you'll excuse me, I must go set objects to Nothing until my hands bleed. :LOL:

Thanks everyone for your expertise, you've helped up my game.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,221,526
Messages
6,160,340
Members
451,637
Latest member
hvp2262

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