Microsoft Office Tutorials and References
In Depth Information
The CommandButton is a basic staple of just about any UserForm.
The combination of a Caption property and Click event make
CommandButtons an efficient way to convey an objective and then
carry it out with a mouse click. And if for no other reason, a Cancel
or Exit button is about as basic a need as any form will have.
Suppose you want to provide your users with a quick way to print a
worksheet in either portrait or landscape orientation. You can make
it easy for your users to click a button to indicate their decision, and
then just go ahead and execute the print job. Figure 19-1 shows
an example of how this may be done, followed by the code behind
each of the CommandButtons.
Private Sub cmdPortrait_Click()
.PageSetup.Orientation = xlPortrait
Private Sub cmdLandscape_Click()
.PageSetup.Orientation = Landscape
Private Sub cmdCancel_Click()
As you can see in the preceding code, each of the CommandButtons has been
named using the prefix “cmd” followed by a notation that gives a clue as to the
purpose of the button (see cmdPortrait_Click() , cmdLandscape_Click() ,
and cmdCancel_Click() ). There is nothing sacred about the “cmd” prefix for
CommandButtons, or about the “lbl” prefix when naming Labels, or about
any naming prefix for that matter. Still, it’s wise to name your controls in some
intuitive and consistent way so you and others will recognize the control and its
purpose when reviewing your VBA code.
You’ve seen Label controls, such as the examples in Lesson 18, where the Label’s Caption property
is set to always display the same text. Sometimes, a Label can serve to display dynamic information
that is not a static piece of text, and in that case, you’d leave the Caption property empty.