Microsoft Office Tutorials and References
In Depth Information
Understanding UserForm Events
‘ Make sure a name is entered
If TextName.Text = “” Then
MsgBox “You must enter a name.”
Exit Sub
End If
The finished dialog box
After making all these modifications, you’ll find that the dialog box works flawlessly. (Don’t
forget to test the hot keys.) In real life, you’d probably need to collect more information than just
name and sex. However, the same basic principles apply. You just need to deal with more
UserForm controls.
A workbook with this example is available on the companion CD-ROM in a file named
get name and sex.xlsm .
Understanding UserForm Events
Each UserForm control (as well as the UserForm itself) is designed to respond to certain types of
events, and a user or Excel can trigger these events. For example, clicking a CommandButton
generates a Click event for the CommandButton. You can write code that is executed when a
particular event occurs.
Some actions generate multiple events. For example, clicking the upward arrow of a
SpinButton control generates a SpinUp event and also a Change event. When a UserForm is
displayed by using the Show method, Excel generates an Initialize event and an Activate
event for the UserForm. (Actually, the Initialize event occurs when the UserForm is loaded
into memory and before it’s actually displayed.)
Excel also supports events associated with a Sheet object, Chart objects, and the
ThisWorkbook object. I discuss these types of events in Chapter 18.
Learning about events
To find out which events are supported by a particular control, do the following:
Add a control to a UserForm.
Double-click the control to activate the code module for the UserForm.
The VBE will insert an empty event-handler procedure for the default event for the
Search JabSto ::

Custom Search