Viewing 5 posts - 1 through 5 (of 5 total)
  • VBA geek question
  • footflaps
    Full Member

    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….

    acjim
    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.

    footflaps
    Full Member

    ok, so If I use:

    Dim fred as cType
    Set fred = New cType

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

    CharlieMungus
    Free Member

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

    footflaps
    Full Member

    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?

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘VBA geek question’ is closed to new replies.