Microsoft Office Tutorials and References
In Depth Information
Draw a CommandButton in the lower-right corner of the UserForm. Name it cmdExit and
caption it as Exit .
Double-click the cmdExit button, which will take you into the UserForm’s module, with the
cmdExit button’s Click event ready for your code. Type Unload Me , and your UserForm
module in the VBE will look like Figure 21-11.
Insert a class module. From the menu bar, click Insert ➪ Class Module and accept the default
name of Class1. Your cursor will be blinking in the Class1 module’s Code window.
The purpose of this particular class module is to capture an event that is associated with
OptionButton controls. At the top of the Class1 module, publicly declare a variable that
refers to the group of OptionButtons you will involve in the class module code. In that same
statement, expose the events associated with OptionButtons using the WithEvents keyword.
The following statement accomplishes this task:
Public WithEvents OptGroup As msforms.OptionButton
There is nothing special about the OptGroup variable name; you can give your
class module variable whatever name makes sense to you. What makes sense to
me is that I am grouping some OptionButton controls for a demonstration, so
OptGroup is an intuitive name.
To demonstrate the point of this lesson, you can use the Click event for your OptGroup
class. A Message Box will display the name and caption of the OptionButton that was clicked
if that OptionButton is included in the class. Figure 21-12 shows how the VBE will look after
inputting the following class module code.