Consulting

Results 1 to 5 of 5

Thread: Clearing / Resetting Variables

  1. #1
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location

    Clearing / Resetting Variables

    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?

  2. #2
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    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

  3. #3
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    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.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by HTSCF Fareha View Post
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    Thanks for your input Paul. Much appreciated.

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •