The VBA keyword "Nothing" marks a variable as having no value, effectively releasing the memory it was using. You use it in assignment statements like Set MyObject = Nothing to clear object references and prevent memory leaks. This guide explains how to use it correctly, why it matters, and how to avoid common errors associated with it.
What You'll Need
- Microsoft Excel installed on a Windows PC (VBA is not available on Excel for Mac in the same way).
- Visual Basic Editor (usually accessed via the Developer tab or by pressing
Alt+F11). - A basic understanding of Object Variables (variables that hold references to things like Worksheets, Charts, or Range objects).
Step-by-Step
- Open the VBA Editor:
Launch Excel and press
Alt+F11to open the Visual Basic Editor. In the menu bar, go to Insert > Module to create a new code module where you will type your code. - Declare an Object Variable:
Create a variable specifically for an object type. Unlike a standard number or text variable, object variables must be declared with the
Setkeyword. Paste this into your new module:Dim ws As WorksheetThis declares a variable named
wsthat can hold a reference to a Worksheet object. - Assign a Value to the Variable:
Use the
Setkeyword to assign a real object (like the active worksheet) to the variable. Add this line of code immediately after the declaration:Set ws = ActiveSheetAt this point,
wsis not "empty"; it points to the currently active sheet in your workbook. - Use the Variable:
Perform an action using the variable to confirm it works. For example, you can change the name of the active sheet using the variable reference:
ws.Name = "My New Sheet Name"Now,
wsholds a live pointer to that specific sheet. - Assign Nothing to the Variable:
This is the core step. To release the object reference and free up memory, assign the constant
Nothingto the variable using theSetstatement:Set ws = NothingAfter this line runs,
wsno longer points to the worksheet. It is now "Nothing." You cannot perform operations on it (like changing its name) because it has no target. - Clearing Memory in Long-Running Macros:
If you are writing a loop that creates and destroys many objects, manually setting them to Nothing at the end of each iteration is a best practice. This prevents your macro from slowing down or crashing Excel as it consumes more memory over time.
Common Mistakes
One of the most frequent errors when using VBA is trying to assign Nothing without the Set keyword. Because Nothing is used to clear object references, you must use Set to assign it. If you write ws = Nothing, VBA will throw a Type Mismatch error. You must write Set ws = Nothing.
Another mistake is assuming that setting a variable to Nothing destroys the underlying object itself. It does not. It simply breaks the link between the variable and the object. The worksheet (or range, or chart) remains in Excel, with all its data and formatting intact. The variable is just left pointing to "Nothing."
Tips
Always use Option Explicit at the top of your modules. This forces you to declare all variables, which helps catch typos where you might have meant to write Set ws = Nothing but accidentally wrote Set ws = NotHing. A typo here will cause a runtime error.
When debugging, you can check if a variable is Nothing by using an If statement. This is useful for preventing errors when you try to access a property of a variable that hasn't been assigned yet. For example:
If Not ws Is Nothing Then
' Code here will only run if ws has a value
ws.Delete
End If
This pattern is essential when writing reusable functions that might be passed a variable that the caller hasn't initialized.
Troubleshooting
If your code throws a Run-time error '91': Object variable or With block variable not set, it usually means you are trying to use an object variable that has been assigned Nothing (or was never assigned at all). The code is trying to do something, like ws.Name = "Name", but ws is currently empty.
To fix this, you must ensure you assign a valid object to the variable before trying to use it, or wrap your code in an If Not MyObject Is Nothing check to see if it exists before proceeding.
Frequently Asked Questions
Does assigning Nothing to a variable delete the data in Excel?
No. Assigning Set MyObject = Nothing only clears the memory reference held by the VBA variable. It does not delete the worksheet, range, or chart that the variable was pointing to. The data remains in your workbook until you explicitly delete it or close the file.
Why do I get a Type Mismatch error when I try to use "Nothing"?
You get a Type Mismatch error because Nothing is a special constant used specifically for object variables. If you try to assign it to a standard variable (like a String or Integer), VBA does not know how to handle the assignment. You must use the Set keyword to assign Nothing to an object variable.
Is it necessary to set variables to Nothing in every macro?
For short macros that run once and finish, it is not strictly necessary, but it is good practice. For long, complex macros that run repeatedly or handle large workbooks, setting variables to Nothing at the end of the procedure is highly recommended to prevent memory leaks and ensure Excel remains responsive.
Can I use Nothing with non-object variables?
No. The Nothing constant can only be assigned to variables declared as Objects (like Worksheet, Range, Shape, or Application). You cannot set a String or Integer variable to Nothing; instead, you simply assign an empty value like MyString = "" or MyInt = 0.
What is the difference between clearing a variable and releasing memory?
When you set an object variable to Nothing, you are effectively telling VBA that you no longer need that specific link to the object. VBA then marks that memory space as available for reuse. This is the process of freeing memory. If you do not set it to Nothing, that memory space remains allocated to your variable until the macro finishes, even if you aren't using it anymore.