The Excel VBA Masterclass, what to expect part 1
Discussing OOP and Class modules
We’ll start this subject by talking a bit about what Object Oriented Programming means and how this is implemented in VBA.
OOP in VBA uses class modules so we’ll dig into that some more.
Types of classes
We’ll distinguish class types by their function, touching on these three: Event classes, Object classes, and Classes of classes.
An event class is a class that is mainly created to handle events. Any VBA programmer will sooner or later get acquainted with the worksheet modules and the ThisWorkbook modules and use them to have VBA code respond to events in the workbook or worksheet in question. Note that these events are “local” in such a sense that the only events you can respond to are events in the workbook your VBA code is a part of. But in some situations you need to trigger code when something happens at a higher level, for example when a workbook is opened or closed in Excel. The only way to respond to such events is by writing a class module.
Classes are not just for handling events. In fact, handling events is just a tiny part of what classes can do for you. In this chapter I will demonstrate a class which enables you unprotect a worksheet in a workbook and which automatically protects it when your code ends.
Classes of Classes
A situation which regularly arises is one where you have the collection-item combination. For example, a workbook has a collection of worksheets. In this part of the course we demonstrate how you set up the necessary plumbing to make this work reliably.
Up next: Efficient coding
On our next page we discuss what our course will bring regarding efficient coding.