Freeing an Object Variable the Nothing Keyword

To free an object variable so that it no longer points to anything, we use the Nothing keyword, as in:

Set rng2 = Nothing

It is good programming practice to free object variables when they are no longer needed, since this can save resources. An object variable is also set to Nothing automatically when its lifetime expires.

Note that once an object no longer has any references to it, the object will automatically be destroyed by VBA, thus freeing up its resources (memory). However, all references to the object must be freed before the object is destroyed. This is another reason not to point more than one object variable at the same object if possible.

0 0

Post a comment