How to close a UserForm in Excel. This includes hiding the form as well as completely closing it and removing it from memory.
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:
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.
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:
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.
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.
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.