Microsoft Office Tutorials and References
In Depth Information
Creating Your Own Objects
There is a one-to-one relationship between a class and a class module. A
class module provides for only one class to be defined. If you need to define
three classes in your workbook, you’ll need three class modules, one for each
class. For example, suppose you have several CheckBox controls on your
UserForm, and you want to color the CheckBoxes green when they are checked,
and red when they are unchecked. Instead of coding this functionality for
every CheckBox’s Click event, you can use a class module that groups all the
CheckBoxes as a single collection object. That way, all CheckBoxes respond to
the same Click event, with one VBA class procedure. If you also want some (or
all) of the CommandButtons on a UserForm in that same workbook to respond
to, say, a MouseMove event, you’d create another class module for that.
crEATing your oWn oBjEcTs
I started this lesson saying that many VBA programmers have avoided the topic of class modules,
and it wouldn’t surprise me if a primary culprit is VBA’s intentionally vague concept of class objects.
Seeing actual VBA examples of class modules in everyday situations is the best way to pick up the
concept of class objects.