VBA- speed- releasing objects

Dear Experts:

I write code in VBA for Excel and Access. I object have simple objects which refer to the Active Worksheet or Active Database, that type of thing. I'm trying to optimize my code for speed.

I saw somewhere on the web that it helps at the end of each Sub to "release" each object. Do you agree? I just don't want to start doing this if it doesn't really help anything.

Dim AA
Set AA = ActiveSheet
Set AA = Nothing  'this it the "release"

What does everyone think?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hello tobal99z,

cleaning up is always a very good practice, it doesn't really speed things up, but shouldn't slow it either

the most important benefit is that it prevents some possible hours of debugging when you do not release your objects and reuse them later when the program is still using the old reference and your logic is expecting something else

other benefit is that it keeps the operating system clean, windows will clean up unused references itself, but helping it can prolong the longevity of the session

hope this helps a bit and a happy new year!
Although it is a good practice to do this, it won't speed up your code. In this case its even useless if your goal is to free memory. Its useless because setting your variable to Nothing, will not release the memory its pointing to. ActiveSheet will always exist.

Nonetheless, you should keep on doing this. Setting a variable to Nothing, basically tells everyone reading the code "I'm done with this variable. see?"
A previous question I have contributed to on this subject:

"Setting Object Variables to Nothing When You're Done - Is It Necessary?"
[ http://www.experts-exchange.com/Applications/MS_Office/Excel/Q_21489443.html ]



"Optimizing Code"
[ http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconoptimizingcode.asp ]

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

I've read both. good information and its all true.

I think you're focussing on VBA in Excel a little to much. If you have a reference to an Excel object, then you cant free it of course. I do think this is a good practise in general. I even do it in c#, even when I know there's a garbage collector who does it all for me.

As for your example in your previous post:
Sub MySub
    Dim ObRef as Object
    Set  ObRef = MyObject
    MsgBox ObRef.Name
    Set ObRef = Nothing
End Sub

It *is*neccessary in vb to do this. If you don't do this your memory will never be freed. It *is not* neccessary in vb.net because the garbage collector will automatically disgard memory if it's not in use.
"I think you're focussing on VBA in Excel a little to much"
I think you'll find this is because the previous question was concerning MS-Excel VBA code.

The "Optimizing Code" link is VB-biased, not Excel/VBA centred.

I also set objects to Nothing as a matter of course, whether or not the compiler/interpreter is going to recover/collect anyway, and I am still with you that it is necessary within VB to do this (but I haven't written pure VB code for at least 10 years, so can only comment on the fact that certainly up to VB 5.0, objects needed to be set to Nothing).

I'm not sure what you were trying to prove with your "MySub" sample, though.



tobal99zAuthor Commented:
Thank you guys! was very helpful!
Thanks for closing the question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.