Sei sulla pagina 1di 3

VBA UserForms

by Kenny Ramage

If you have programmed w th AutoL sp and the D alogue Control Language (DCL), you w ll be fam l ar w th des gn ng
d alogue boxes. Compared to DCL though, des gn ng d alogue boxes n VBA s a breeze. The bas s of all d alogue boxes n
VBA s the UserForm. Th s Tutor al w ll show you how to d splay and man pulate UserForms.

A UserForm s a conta ner that holds all the controls such as labels, textboxes, p ctures, etc. that make up part of your
appl cat ons nterface.
A UserForm has t's own Propert es, Methods and Events. Let's have a look at some of them :

Displaying a UserForm
The syntax for d splay ng a UserForm s as follows :

UserFormName.Show

So, to d splay a UserForm named UserForm1, you would use the follow ng code :

UserForm1.Show

If you want to, you can preload the UserForm nto memory w thout actually d splay ng t. Th s can be useful as t can
somet mes take a few seconds for a complex UserForm to appear. The ab l ty to preload the UserForm allows you to dec de
when you would l ke th s operat on to take place. To preload a UserForm you would use the follow ng code :

Load UserForm1

Hiding/Unloading a UserForm
To temporar ly h de a UserForm, you would use the H de method. Th s s a very good example of how d alogue boxes n
VBA are so much s mpler than n AutoL sp. To h de a UserForm you would use the follow ng code:

UserForm1.Hide

To Unload a UserForm from memory use the follow ng code :

Unload UserForm1

You could also use the "Me" keyword :

Unload Me

Useform Events
UserForms support many predef ned events. Among the most commonly used events are In t l aze, Cl ck and Term nate
events.

Note: A VBA module that conta ns an event procedure can be called a module beh nd the UserForm. A module that
conta ns event procedures s not v s ble n the Modules collect on of the Projects w ndow of the VBA Ed tor. You must
double-cl ck the body of the UserForm to v ew the UserForm Code Module.

Let's have a look at some UserForm Events. Start your VBA Ed tor and nsert a UserForm nto a new Project. Double-Cl ck
the UserForm and type n the follow ng code :

Private Sub UserForm_Click()


Me.Height = Int(Rnd * 500)
Me.Width = Int(Rnd * 750)

End Sub

Private Sub UserForm_Initialize()


Me.Caption = "UserForm Events"
Me.BackColor = RGB(10, 25, 100)

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)


msg = "Now Unloading " & Me.Caption
MsgBox prompt:=msg, Title:="QueryClose Event"

End Sub

Private Sub UserForm_Resize()


msg = "Width: " & Me.Width & Chr(10) & "Height : " & Me.Height
MsgBox prompt:=msg, Title:="Resizing Event"

End Sub

Private Sub UserForm_Terminate()


msg = "Now Unloading " & Me.Caption
MsgBox prompt:=msg, Title:="Terminate Event"

End Sub

Now run the UserForm. Th s s what happens when you run the project:

F rstly, The Int al ze event procedure changes the Capt on property to "UserForm Events" and the Backcolor property to
dark Blue.

When you cl ck the UserForm the Cl ck Event procedure s n t ated and the UserForm s re-s zed. Also, because you
created a res ze event procedure, you rece ve 2 message boxes. The res ze event occurs tw ce because your code beh nd
the cl ck event changed both the W dth and He ght propert es of the UserForm.

When you close the UserForm, the QueryClose event procedure s tr ggered. Th s d splays a message box w th the capt on
you gave the UserForm n the code for the In t al ze event. The QueryClose event s useful when you want to perform a
certa n set of act ons when the UserForm s closed by the user.

The Term nate Event then tr ggers a message box wh ch states that the Capt on of the UserForm s UserForm1. The
Term nate Event occurs after the UserForm s removed from memory and the Capt on of the UserForm returns to t's or g nal
state.

Potrebbero piacerti anche