Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a Userform with Unload Me doesn't work

Tags:

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 
like image 294
Kian Avatar asked Feb 28 '12 22:02

Kian


People also ask

How do I close a UserForm?

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.

What is the function of the Unload Me statement?

“Unload Me” closes your form and removes everything associated with it from memory.

How do I remove a UserForm in Excel?

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...


1 Answers

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.

  1. You could be entering the code in the wrong place (such as a separate module)

  2. 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.

like image 109
user4059073 Avatar answered Oct 06 '22 09:10

user4059073