VBA Open or Close UserForm

Associated Files Download Links

In this tutorial, you will learn how to initialize, open and close a Userform using VBA.

We have a simple Userform called basicUserform shown below with a label, a textbox, and three command buttons.

Basic UserForm in VBA

Open a Userform using VBA

Use the following code to open the Userform called basicUserform:



Close a Userform using VBA

You can close a form using the Unload keyword:

Unload basicUserform

You can also use the Me keyword to close a specific form:

Unload Me

Note: You can only use Unload Me in procedures contained in the Userform Code Module:

userform unload me


You can access the UserForm Code Module by double-clicking on the module in the Code Explorer (on the left). Or by right-clicking in the UserForm visual editor.

Notice in the example above we added “Unload.Me” to the “Click” event of the Cancel button. So when the user clicks the Cancel button, the form will unload.


Initialize a Userform in VBA

When a form is loaded, the “Initialize” event is triggered. You can use this event to change the UserForm appearance such as populating combo boxes or turning controls on/off in your initialization code.

This code will disable the Cancel button when the UserForm is launched:

Private Sub UserForm_Initialize()

cmdCancel.Enabled = False

End Sub


Using the Initialize Event in VBA

Note: This code must be placed in the UserForm code module (see picture above).