Closing a UserForm

Add to Favorites
Author:

How to close a UserForm in Excel. This includes hiding the form as well as completely closing it and removing it from memory.

Sections:

Close the UserForm

Hide the UserForm

Red X

Notes

Close the UserForm

This will completely close the UserForm and clear it from memory. When you do this, anything that was entered into the form will be removed and any changes made to the form will be cleared. Don't let that scare you though, this is the normal way that you close a UserForm.

This is the code that you use:

Unload Me

You put this into the code section of the UserForm.

In the VBA window (Alt + F11), double-click the button that you want to close the form; this causes a code window to appear; in that window, paste the above code.

It will look something like this:

9b49f9f7aa4b064ec0dad97c48cec997.png

You can also close the form using its name instead of "Me" and that would look like this:

Unload UserForm1

In this example, the name of the form is UserForm1.

Hide the UserForm

This makes the form disappear but it does not clear it from memory or actually close the form. This means that anything that was entered into the form and any changes made to it will remain in the form.

Me.Hide

Put this code into the button that you want to cause the form to be hidden.

In the VBA window (Alt + F11), double-click the button that you want to hide the form; this causes a code window to appear; in that window, paste the above code.

It will look something like this:

ee7d64bca3244bacaec4f204451a9c3d.png

If you put this piece of code outside of the UserForm, you will need to reference the name of the form like this:

UserForm1.Hide

UserForm1 is the name of the form to hide.

Red X

Click the red X in the upper-right corner of the UserForm.

This is the way to close a UserForm that doesn't have any buttons on it or that hasn't had a "close" button programmed into it.

Notes

Closing a UserForm is, as you can see, very easy to do. However, make sure that when you input the code to close the UserForm, you do it in an intuitive way. For instance, if there is a button that closes the form, make sure the button looks like that's what it does, such as by saying "Close" or "Cancel".

Download the sample workbook for this tutorial to see this in action.

Question? Ask it in our Excel Forum


Downloadable Files: Excel File

Similar Content on TeachExcel
Make a UserForm in Excel
Tutorial: Let's create a working UserForm in Excel. This is a step-by-step tutorial that shows you e...
Showing a UserForm
Tutorial: How to display a UserForm in Excel and some common things you might want to do when you di...
Reset All Values in a UserForm
Tutorial: Clear or reset all values in a UserForm so that they contain only the default original val...
What is a UserForm in Excel?
Tutorial: A UserForm is basically a pop-up window that you can use to create a custom interface for ...
Loop Through Controls in a UserForm
Tutorial: How to loop through all controls in a UserForm; this allows you to do things like get valu...
Getting Data from a UserForm
Tutorial: How to get data from a UserForm in Excel, including from: text inputs (TextBox), list boxe...