Introduction | Example | Tutorial | Applications
Introduction - VBA Close UserForm
Close a UserForm with VBA by using the Unload Me VBA statement. You can also unload userforms with different names by replacing “Me” with the name of your form.
I’ll show you how to close userforms with VBA in this brief tutorial from the VBA Tutorials Blog. At the bottom, you’ll see a macro showing how to use a loop to close all your userforms at once.
Example - VBA Close UserForm
Private Sub cbCancel_Click() Unload Me End Sub
Make powerful spreadsheets with our free VBA Developer's Guide This is actually pretty neat. If you have trouble understanding or remembering it, our free VBA Developer Guide can help. It’s loaded with VBA shortcuts to help you make your own macros a whole lot faster - we’ll send a copy to your email address below.
This is actually pretty neat. If you have trouble understanding or remembering it, our free VBA Developer Guide can help. It’s loaded with VBA shortcuts to help you make your own macros a whole lot faster - we’ll send a copy to your email address below.
Tutorial - VBA Close UserForm
Close a UserForm from UserForm Event
The macro above only works when you call it from an event procedure on that UserForm - like a command button. That’s because only the UserForm knows that “Me” is the UserForm.
In other words, if you type Unload Me in a regular module, it will fail with an error saying “Invalid use of Me keyword.” Remember, the module doesn’t know what UserForm you want to unload!
Let’s walk through an example showing where it’s appropriate to use “Unload Me” to close your form. Pretend you have a userform that looks like this:
You may recognize this UserForm from my Mouse To Macro mouse recording Excel Add-in. On this form, I’ve named the Cancel button
When you click the
Your macro can be a lot more complicated than this. Feel free to run whatever commands you want before you put the
Unload Me statement. “Unload Me” closes your form and removes everything associated with it from memory.
Close UserForm from another Module
This isn’t the first time you’ve seen me use the Unload statement. I used it to demonstrate my VBA Excel splash screen, but if you recall, I didn’t use the “Me” keyword. I could have, but I didn’t.
Let’s say you have a modeless UserForm (ShowModal property set to False), titled
Anyway, pretend you have this modeless UserForm floating around. You have several other macros that run while the UserForm is just chilling there. To close your UserForm from one of these macros, you would pair “Unload” with the name of your UserForm, like this:
Sub UnloadFormModule() Unload UserForm1 End Sub
Because the UserForm was named
Close All UserForms with VBA
If you want to close all your UserForms, you could create a loop that loops through each form and unloads each form - one by one. Here’s an example:
Sub UnloadAllForms() Dim tempForm As UserForm For Each tempForm In UserForms Unload tempForm Next End Sub
Application Ideas - VBA Close UserForm
I’m going to leave you with an application idea to motivate, inspire and challenge you. A long time ago, I created a modeless UserForm that stayed on the screen until the user pressed a certain key. Once that key was pressed, the form was closed.
Can you do that? Leave a comment below with your macros demonstrating how to do it!
If you haven’t already done so, join our VBA Insiders using the form below. After that, share this article on Twitter so your friends can learn.