Tutorial Details
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

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