Introduction | Example | Tutorial | Applications | Comments
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
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!
Now’s the time I kindly remind you to subscribe to my email list. Once you’ve subscribed, you’ll get access to a free copy of my Excel Add-In with tons of useful features and you’ll get monthly tutorials where I show you how to do some pretty cool stuff with VBA:)
About Ryan Wells
Ryan Wells is a Nuclear Engineer and professional VBA Developer. He is the lead developer of several VBA applications, including PDF VBA - a leading Excel Add-in for exporting Excel Objects, like charts and tables, to PDFs. Discover more of his popular Excel Add-ins, including Mouse To Macro and CF Shapes, at his dedicated Excel Add-ins page.Follow