CHAPTER 20Leveraging Class Modules

What Is a Class Module?

For many VBA programmers, the concept of a class module is a mystery. This feature can be confusing, but the examples in this chapter will help to make it less mysterious.

A class module is a special type of VBA module that you can insert in a VBA project. Basically, a class module enables the programmer (you) to create a new object. As you should know by now, programming Excel really boils down to manipulating objects. A class module allows you to create new objects, along with corresponding properties, methods, and events.

At this point, you might be asking, “Do I really need to create new objects?” The answer is “No.” You don't need to, but you might want to after you understand some of the benefits of doing so. In many cases, a class module simply serves as a substitute for functions or procedures, but it could be a more convenient and manageable alternative. In other cases, however, you'll find that a class module is the only way to accomplish a particular task.

The following are some typical uses for class modules:

  • To encapsulate code and improve readability By moving all of your code related to payroll, for example, into custom objects representing employees and paychecks, you can keep your code more organized.
  • To handle events of objects ...

Get Excel 2019 Power Programming with VBA now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.