+ Reply to Thread
Results 1 to 4 of 4

Garbage Collection using VBA

  1. #1
    Registered User
    Join Date
    11-16-2004
    Posts
    38

    Garbage Collection using VBA

    Hello,

    In some programming languages I use, garbage collection is taken care of automatically by the program,
    in others, the programmer is responsible for this.

    With Excel VBA, which is the case?

    ie if I do the following:
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = Sheets("Calculator Data Table")
    Set ws2 = Sheets("W-Shape MC")

    Must I do the following when I am finished with them?
    Set ws1 = Nothing
    Set ws2 = Nothing


    Similarly with:
    Dim MyRange
    MyRange = "A1:" & Selection.SpecialCells(xlCellTypeLastCell).Address

    Is the following required?
    Set MyRange = Nothing
    Last edited by mms; 04-18-2019 at 08:48 AM.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Garbage Collection using VBA

    Not required. Most objects are destroyed when it goes out of context (i.e. when the sub finishes execution).

    However, there are some external objects that will require such handling (either set to nothing, or explicitly closed etc).

    You can read more about in link below.
    https://blogs.msdn.microsoft.com/eri...ts-to-nothing/
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,468

    Re: Garbage Collection using VBA

    Variables declared within a sub will be destroyed when that sub ends so there is little point in setting to nothing in most cases.

    However, if you're passing variables from one sub to another and have therefore declared them at module level (outside of a sub) or they're being used even further afield so declared as public or even global then they will not clear when a sub finishes but hang around until the workbook/application is closed.

    In those cases it's often a good idea to set to nothing to preserve memory for other things. However I don't think this is such an issue with modern computers (not like back when I first started learning programming languages and was limited to about 48k of memory rather than gigabytes of the stuff!

    One of the times I always set variables to nothing is when I have Excel talking to Access through VBA as leaving connections open can cause problems.

    BSB

  4. #4
    Registered User
    Join Date
    11-16-2004
    Posts
    38

    Re: Garbage Collection using VBA

    OK Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] When a collection created bring another column value into the collection
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-21-2018, 04:33 AM
  2. [SOLVED] Issue - Garbage on my screen
    By kev_ in forum Suggestions for Improvement
    Replies: 13
    Last Post: 02-23-2017, 03:26 PM
  3. Replies: 4
    Last Post: 10-30-2015, 01:30 PM
  4. [SOLVED] Performance of iteration through Words collection degrades as collection gets bigger
    By 6StringJazzer in forum Word Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2014, 09:44 AM
  5. Replies: 2
    Last Post: 12-23-2011, 11:04 AM
  6. Garbage collection in VBA
    By Edward Ulle in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-16-2005, 02:00 PM
  7. Deleting garbage
    By Sandra in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-05-2005, 01:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1