VBA Runtime Error 364 "Object was unloaded" occurs when your code attempts to access an object that is no longer held in the computer's memory. This typically happens because the object was explicitly destroyed using the "Set = Nothing" command, the userform containing the object was closed/unloaded, or a global variable lost its reference due to an unhandled error in another part of the code. To fix this, you must ensure the object is properly instantiated before use and avoid unloading the parent container while the object is still required.

What You'll Need

  • The Excel, Word, or Access file containing the VBA project.
  • Access to the Visual Basic Editor (VBE).
  • A backup copy of your file (recommended before modifying code).

Step-by-Step

  1. Open the Visual Basic Editor: Press Alt + F11 in your application to open the VBE.
  2. Locate the Error Line: Run your code until the error triggers. When the dialog box appears, click "Debug." The VBE will highlight the specific line of code causing the crash in yellow.
  3. Check for "Unload Me" Commands: If the error occurs within a UserForm, look for Unload Me. If you unload a form but then attempt to reference a control (like a TextBox or ComboBox) on that form later in the code, Error 364 will trigger. Change Unload Me to Me.Hide if you need to keep the data in memory while the form is invisible.
  4. Verify Object Initialization: Ensure the object is not Nothing before calling its methods. Insert a check before the crashing line:
  5. Audit "Set = Nothing" Statements: Search your project for Set [ObjectName] = Nothing. Ensure you aren't clearing the object in one subroutine while another subroutine still expects it to be active.
  6. Check for Global State Reset: If the error happens randomly, it may be because a previous error occurred elsewhere in the code, which resets all global and module-level variables. Add On Error GoTo [ErrorHandler] to your procedures to prevent the VBA project from resetting unexpectedly.
  7. Refresh Project References: Go to Tools > References. Look for any items marked as "MISSING:". Uncheck the missing references and find the correct updated version of the library to restore the connection.

Common Mistakes

  • Confusing Hide with Unload: Using Unload completely removes the object from memory, whereas Hide simply removes it from the user's view. Attempting to read a value from a hidden form works; attempting to read from an unloaded form triggers Error 364.
  • Over-cleaning Memory: While setting objects to Nothing is good practice for memory management, doing it too early in a complex sequence of calls often leads to this error.
  • Implicit Declarations: Using Option Explicit is often ignored, but without it, a typo in an object name can create a new, empty variable that is immediately "unloaded" or null, leading to confusing runtime errors.

Tips

  • Use Module-Level Variables: If an object needs to persist across multiple subroutines, declare it at the very top of the module (outside any Sub or Function) using the Public or Dim keyword.
  • Implement Error Trapping: Use On Error Resume Next sparingly, but specifically around object calls that might be unstable, followed immediately by a check to see if an error occurred.
  • Naming Conventions: Prefix your objects (e.g., objWorkbook or frmMain) so you can easily track where they are being instantiated and destroyed throughout your project.

Troubleshooting

If the steps above do not resolve the issue, try the following in order:

  • Restart the Application: Sometimes the VBA project becomes corrupted in the current session. Save your work, close the application entirely, and restart.
  • Compile the Project: In the VBE, go to Debug > Compile VBAProject. This will force the editor to find syntax errors or missing references that might be causing objects to fail during runtime.
  • Export and Import Modules: If the error persists despite correct code, the module itself may be corrupted. Right-click the module in the Project Explorer, select Export File..., remove the module from the project, and then Import File... to bring it back.
  • Check for Third-Party Add-ins: Disable any active Excel/Word add-ins to ensure a conflict isn't unloading your objects from the background.

Frequently Asked Questions

Why does this happen only sometimes?

This usually happens because of "state." If a different part of your code crashes or is stopped manually by the user, VBA resets all global variables to Nothing, meaning the object is unloaded even if your logic says it should still be there.

Is "Object was unloaded" the same as "Object variable not set"?

No. Error 91 (Object variable not set) means the variable was never pointed to an object. Error 364 means it was pointed to an object, but that object has since been deleted from memory.

Can this be caused by a computer memory issue?

Rarely. While it sounds like a RAM issue, it is almost always a logical error in the code where a Set = Nothing or Unload command was executed prematurely.

Will updating Office fix this error?

Updating Office generally does not fix Error 364 because it is a coding logic error, not a software bug. However, updating can fix "MISSING" reference errors which may contribute to the problem.

How do I prevent this in large projects?

The best prevention is to use a "Getter" function. Instead of referencing a global object directly, create a function that checks if the object is Nothing; if it is, the function re-instantiates the object before returning it to the main code.