VBA geek question
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] VBA geek question

4 Posts
3 Users
0 Reactions
87 Views
Posts: 13594
Free Member
Topic starter
 

Do I need to release objects created inside functions eg

Public Function Fred(..) As ...

'Create copy of WS
Dim WS As Worksheet
Set WS = Worksheets("Network")

{do stuff here}

End Function

Do I need to release WS on exit eg add Set WS = Nothing - I seem to find conflicting advice on t'interweb.

I'd have thought as it was defined local to the function it will be garbage collected on exit....


 
Posted : 14/01/2011 11:24 am
Posts: 0
Free Member
 

I've found in the past that if you're dealing with new application objects (excel etc) then it's better to fully release as they can persist, same goes for creating completely new objects that you don't want hanging about (temporary forms etc). In your example I wouldn't have thought it was necessary as you aren't creating new objects just aliasing existing ones.


 
Posted : 14/01/2011 11:28 am
Posts: 13594
Free Member
Topic starter
 

ok, so If I use:

Dim fred as cType
Set fred = New cType

then I have to Set fred = Nothing before exiting the function?


 
Posted : 14/01/2011 11:35 am
Posts: 0
Free Member
 

I think you'll be ok, 'cos it's local. But I tend to clear them anyway, just to be sure.


 
Posted : 14/01/2011 11:44 am
Posts: 13594
Free Member
Topic starter
 

Just run some tests, creating millions of new objects by calling the same function in a loop and Excel's memory allocation seems unchanged. Not sure there is anyway to see the actual Stack or Heap size for VBA itself?


 
Posted : 14/01/2011 11:55 am