I need to close an Excel userform using VBA when a user has clicked a submit button and operations have been carried out.
How can I close a Userform from itself?
I have tried this but it returns a 361 error.
Unload Me
Once the purpose of the user form is done, there is a point in keep showing the userform in front of the user, so we need to close the userform. We can close the userform by using the “Unload Me” statement and “UserForm. Hide” statements.
“Unload Me” closes your form and removes everything associated with it from memory.
You should see something like FORMS then under that Userform1 Right click Userform1 then Remove. Alternately, activate the UserForm window and then click the File item on the VBA editor menu bar and find Remove UserForm...
As specified by the top answer, I used the following in the code behind the button control.
Private Sub btnClose_Click() Unload Me End Sub
In doing so, it will not attempt to unload a control, but rather will unload the user form where the button control resides. The "Me" keyword refers to the user form object even when called from a control on the user form. If you are getting errors with this technique, there are a couple of possible reasons.
You could be entering the code in the wrong place (such as a separate module)
You might be using an older version of Office. I'm using Office 2013. I've noticed that VBA changes over time.
From my experience, the use of the the DoCmd.... method is more specific to the macro features in MS Access, but not commonly used in Excel VBA.
Under normal (out of the box) conditions, the code above should work just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With