UserForm Code Reference
UserForm Code Reference
Closing a UserForm
There are two ways to close a UserForm. You can Unload it or Hide it. Such procedures are usually
run from a button on the form itself.
When using the Unload method from in the form itself you can refer to the form using the keyword
Me. If you are referring to a different form you must refer to it by its name:
or
Unload Me
Unload UserForm1
The Unload statement closes the form, removes it from the computer's memory and returns control
of the host file to the user. When the form is next opened it will have lost any data that had been
entered into it.
Using the Hide statement removes a UserForm from view, returning control of the host file to the
user, but the form does not actually close, being retained in the computer's memory until it is
needed again. Use this method if you want to temporarily hide the form so that when it is opened
again it appears exactly as it was when it was hidden with textboxes filled, combobox choices
retained etc. It is written as follows:
Me.Hide
or
UserForm1.Hide
For example, you might want to automatically enter today's date into a textbox. This example uses
the VBA Date function to do that:
Me.TextBox1.Value = Date
Or, you might read existing values from cells on an Excel worksheet:
Me.TextBox1.Value = Worksheets("Sheet1").Range("A1").value
or
Range("Sheet1!A1").Value = Me.TextBox1.Value
Alternatively you can write to a named range in which case it is not necessary to specify the
worksheet since this is implicit in the named range definition:
Range("MyRangeName").Value = Me.TextBox1.Value
Martin Green www.fontstuff.com