O'Reilly logo

Excel 2003 VBA Programmer's Reference by Robert Rosenberg, Rob Bovey, John Green, Stephen Bullen, Paul T. Kimmel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6

Class Modules

Class modules are used in VBA to create your own customized classes. Here are some examples of problems for which you might use a custom class modules:

  • Respond to application events; you can write code that is executed whenever any open workbook is saved or printed, for example
  • Respond to embedded chart events
  • Set up a single event procedure that can be used by a number of ActiveX controls such as text boxes in a UserForm
  • Encapsulate Windows API methods to permit interaction with Windows
  • Encapsulate standard VBA procedures in a form that is easy to transport into other workbooks

In this chapter, we will define some class to get the idea of how class modules work. Then, we will apply the principles to some more useful examples. You are already familiar with Excel's built-in objects, such as the Worksheet object, and you know that objects often belong to collections such as the Worksheets collection. You also know that objects have properties and methods, such as the Name property and the Copy method of the Worksheet object.

Using a class module, you can create your own “blueprint” for a new object, such as an Employee class. You can define properties and methods for the class, such as a Rate property that records the employee's current rate of pay, and a ChangeTitle that implicitly affects other aspects of the Employee's data. You can also create a new collection for the object, such as the Employees collection. The class module is a plan for the objects you want ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required