Chapter 21. Class Modules

Class modules — the very name has caused many an Excel VBA programmer to turn away from the topic as if it doesn't or shouldn't exist. For some reason, the use of class modules is not a skill held by many otherwise knowledgeable VBA programmers, despite the power and flexibility class modules can provide to your workbook projects.

Class modules are not rocket science, but they are a different kind of VBA animal that takes some extra attention to grasp. I want to express three objectives in this lesson:

  • Explain what classes and class modules are.

  • Describe what class modules can do for you.

  • Provide examples of class modules applied to UserForm and embedded worksheet controls.

Here is an opportunity for you to set yourself apart from the VBA crowd and learn a valuable skill that has actually been available in Excel since Office 97. Though you won't need class modules for most of your projects, you'll learn how to recognize when the time is right to use class modules, and most importantly, how to program them.

WHAT IS A CLASS?

A class is the formalized definition of an object that you create. Your first reaction might be to wonder why you'd ever need to create yet another object in Excel, which seemingly has no shortage of objects. Actually, you normally don't need to, but there will be times when your workbook will be better off if you do.

Note

It's easy to get lost on any new topic if the emphasis on learning it is based on definitions and theory. That is why ...

Get Excel® VBA: 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.