PDA

View Full Version : [SOLVED:] Clearing / Resetting Variables



HTSCF Fareha
03-20-2023, 08:40 AM
I have a question regarding the resetting of variables at the end of a sub macro (Hoping that resetting is the correct description!).

For example :-


Set oRng = Nothing
Set oFind = Nothing
Set oCC = Nothing
Set oDoc = Nothing
Exit Sub
End Sub

Would I be correct in saying that it is best practice to do this for each variable that is declared at the start of a sub, or is this not the case and only certain ones need this?

Dave
03-20-2023, 09:13 AM
Hi HTSFC Fareha. I'm going to vote for setting object and range variables to nothing and Erasing arrays. I'm guessing others have more to contribute. Dave

HTSCF Fareha
03-22-2023, 12:33 AM
Thank you, Dave. This makes sense. Trying to reset anything that doesn't fall under either of these results in an error when running the macro in any case. :yes

Paul_Hossler
03-24-2023, 05:51 PM
I have a question regarding the resetting of variables at the end of a sub macro (Hoping that resetting is the correct description!).

Would I be correct in saying that it is best practice to do this for each variable that is declared at the start of a sub, or is this not the case and only certain ones need this?

I believe that the garbage collection kicks in at the End Sub/Function for any variables instantiated within the procedure, so I've been told that it's not necessary

HOWEVER ... I like to do it with those and some other constructs since the 'bracketing' helps me see things




Dim myObj as Class1

Set myObj = New Class1


...
...
...

Set myObject =Nothing



Personal style

HTSCF Fareha
03-24-2023, 11:19 PM
Thanks for your input Paul. Much appreciated.

I think the consensus is to set them to nothing, that's good enough for me!