Unload UserForm VBA: Close Forms and Reset State Safely

VBA & macros · Updated November 18, 2026

In VBA, use Unload UserForm1 to close and unload a form, or use Unload Me from code inside that form. Unloading removes the form from use so that its controls and state are cleared; use Hide instead when you want to close the window temporarily and preserve its current values.

What Unload Does in VBA

A VBA UserForm is a custom window that can display controls such as text boxes, buttons, and lists. Unload closes the form and removes its loaded instance. When the form is loaded again, its controls are initialized afresh, so a text box that held a value before unloading will normally be blank unless your code restores that value.

For a form whose VBA code name is UserForm1, a standard module can unload it with this statement:

Public Sub CloseEntryForm()
    ' Save a copy of the workbook before running a macro.
    Unload UserForm1
End Sub

Replace UserForm1 with the form’s actual code name, not necessarily the text shown in its title bar. The title bar text is the form’s Caption property; the code name is what VBA uses to refer to the form in code. If the statement is in a procedure belonging to the form itself, Unload Me refers to the current form and avoids depending on its name.

VBA macros cannot be undone with Ctrl+Z. Save a separate copy of the workbook before running a macro, especially if the form is part of a larger process that also changes worksheet data.

Unload Me From a Form Button

A common pattern is a Close or Done button on the form. In the UserForm’s code module, double-click the button in the Visual Basic Editor to create its click event, then unload the current form:

Private Sub cmdClose_Click()
    ' Close this form and discard its current control state.
    Unload Me
End Sub

Here, cmdClose must match the button’s actual Name property. The procedure belongs in the form’s code module, not in a standard module. If the form should stay open, use Me.Hide instead. If a button is meant to submit data, read or validate the controls and save the values before unloading; unloading does not automatically write form values to a worksheet.

To open a form, a macro can call its Show method. For example, with the form named UserForm1:

Public Sub OpenEntryForm()
    ' Save a copy of the workbook before running a macro.
    UserForm1.Show
End Sub

For a modal form, which is the default, VBA pauses the calling procedure at Show until the form is hidden or unloaded. A modeless form, shown with UserForm1.Show vbModeless, allows the user to interact with Excel while the form remains open, and the calling procedure continues immediately.

Unload Versus Hide

Hide makes a form invisible but leaves it loaded. Its existing control values remain available, and calling Show on that same form instance displays it again with those values intact. Unload closes and unloads the instance; showing it again loads it anew and runs its initialization process. This difference matters when a form contains a partly completed entry, a selected option, or temporary data that should either be kept or discarded.

  • Use Hide when a user may return to the same form and continue where they left off.
  • Use Unload when the task is finished, the form should release its current state, or a fresh form is wanted next time.
  • Before either action, explicitly save important values if they must survive closing the form.

A frequent misconception is that Hide and Unload are interchangeable ways to close a window. They both make the form disappear, but only unloading resets the loaded form instance. If a hidden form is never shown again, it can also remain in memory longer than intended, so unload it when its retained state is no longer needed.

Form Events and Initialization

Two form events help explain what happens around unloading. UserForm_Initialize runs when the form is loaded, before it is shown for the first time in that loaded instance. It is often used to populate a list or set starting values. After a form is unloaded and loaded again, initialization runs again.

UserForm_Terminate runs when the form object is being terminated. It is not the best place to ask the user to save information or to cancel a close: by that point, the form is being removed. Put validation and user decisions earlier, such as in a button’s click event or the QueryClose event.

UserForm_QueryClose occurs when a form is about to close, including when the user clicks its window close button. Its parameters let code inspect the reason and prevent the close by setting Cancel to a nonzero value. The CloseMode argument distinguishes, among other cases, a close from the form’s control menu and a close initiated by VBA. This is useful when unsaved form entries need a confirmation prompt, but the event should be kept simple to avoid repeated prompts or a close that can never complete.

Default Forms and Explicit Instances

In typical Excel VBA code, referring directly to a form by its code name, such as UserForm1, uses its default instance. This is convenient for a single form: call UserForm1.Show to display it and Unload UserForm1 to unload it. The same default instance can be shown again later, with its initialization behavior following a new load.

More advanced code can create an explicit instance with New. This is useful when an application needs separate copies of the same form, each with independent values. Keep a reference to the particular instance you intend to close; unloading the default instance is not a substitute for unloading a separately created object.

Public Sub ShowSeparateEntryForm()
    Dim frm As UserForm1

    ' Save a copy of the workbook before running a macro.
    Set frm = New UserForm1
    frm.Show vbModeless

    ' Close this instance when the application is finished with it.
    Unload frm
    Set frm = Nothing
End Sub

This example demonstrates the object reference, but its final two lines run as soon as the procedure reaches them, so the modeless form will close immediately. In a real application, put the unload operation at the point where the user’s task is actually finished and make sure the code still has access to the correct instance. Set frm = Nothing releases that variable’s reference; it is not itself a replacement for unloading a visible form.

Common Problems and Checks

If a form does not close as expected, check where the code is running and whether another event is cancelling the close. A QueryClose handler can intentionally prevent closing. Also confirm that the code is targeting the intended form name or object, and that the unload line is actually reached; a validation branch or an earlier error can stop execution first.

  • The form closes but old values return: Check whether the code used Hide rather than Unload, or whether your initialization code restores saved values.
  • Compile error on Me: Me is appropriate inside a form or class module, not a standard module. In a standard module, refer to the form by its code name or an object variable.
  • The wrong form closes: Verify the code name and, when using New, unload the matching object variable rather than a default instance.
  • Close button appears to do nothing: Confirm the button’s event procedure name matches its Name property and that the event code is in the form module.
  • Values are missing after closing: Transfer them to variables, a worksheet, or another storage location before unloading. The form’s controls are not a permanent data store.

When debugging, place a breakpoint on the unload statement and step through the procedure with F8 in the Visual Basic Editor. On Windows, Alt+F11 opens or returns to the Visual Basic Editor in Excel. On Mac, the shortcut can depend on keyboard settings; use the Developer tab’s Visual Basic command where available. Excel for the web can open workbooks containing macros, but it cannot create, edit, or run VBA macros; use the desktop Excel application.

Choosing a Closing Pattern

Choose the closing method based on what should happen to the form’s contents and who controls the closing process. A short decision check prevents many form-state bugs:

  1. If the user may resume the same unfinished task, hide the form and retain its instance.
  2. If the task is complete and the next opening should start fresh, unload the form.
  3. If the form contains data that must be kept, save that data before hiding or unloading.
  4. If multiple copies can be open, track each explicit instance and close the intended one.
  5. If users can click the window’s X, decide whether that route needs the same validation as a Close button.

For a simple single-form tool, a Close button containing Unload Me is usually the clearest pattern. For a workflow where users can temporarily switch back to the worksheet, Hide may fit better. For a data-entry workflow, first validate and transfer the entries, then unload. Separating those actions makes it easier to see whether a missing value is a storage issue or a form-lifecycle issue.

Frequently Asked Questions

What is the difference between Unload Me and Unload UserForm1?
Unload Me closes the form whose code is currently running, so it is suited to code inside that form. Unload UserForm1 targets the form referred to by that code name and is commonly used from another module for its default instance.

Does Unload clear the values in a UserForm?
Unloading removes the loaded instance, so its control state is not retained for the next load. Code that repopulates controls during initialization or from saved data can, however, put values back.

Should I use Hide or Unload to close a UserForm?
Use Hide to make the form disappear while retaining its state for later use. Use Unload when the form is finished and you want a fresh load next time.

How do I unload a UserForm when the user clicks X?
Clicking the form’s X initiates a close, and the form’s QueryClose event can respond to it. Use that event if you need validation or a confirmation; otherwise, the normal close behavior can unload the form.

Can I use Unload Me in a standard module?
No. Me refers to the current object and is available in the form’s own code module, not an ordinary standard module. From a standard module, refer to the intended form or its stored object variable.

Excel Version and Platform Notes

UserForms and VBA are features of desktop Excel, including Microsoft 365 for Windows. The VBA editor and form designer are not available in Excel for the web, so a workbook that relies on a UserForm requires desktop Excel to run that form. Mac desktop Excel supports VBA, though the editor shortcut and some interface details can differ from Windows; use the Visual Basic command on the Developer tab if the keyboard shortcut is unavailable. These platform differences do not change the basic meaning of Unload in VBA.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.