What is a Class? – Basic understanding of a Class Module – Chapter1
- Last updated:Sat, Jul 22, 2006 -
Introduction

The term
Class Module may give you an impression that it must be difficult. There might still be many Excel users who have never used the Class Module as yet because general VBA programs for Excel can be written only using Standard Modules, UserForm Modules or WorkSheet Modules.
However, once youfve got what the Class Module is, it helps to understand what the Object Oriented Programming is. Besides, a Class Module can be used for any kind of programs. It's not very difficult one so let's give it a try.
What can be done with Class Modules?
Class modules allow you to create and use your own object types in your application so...
You can easily write code that works with any workbooks that do not have any code.
Two or more procedures for the event of a command button can be consolidated in one
Makes the code easy to use by concealing logic and data.
The above examples are just some examples. The possibility of the programming is greatly extended by using a Class Module.
Why use Class Modules, anyway?
Classes make your code
development simpler
more manageable
self-documenting
easier to maintain
But...What is a Class?
Everybody must ask this question at first. At least I did....
Simply put, a Class is a Blueprint of an Object. In Excel VBA, an Object can mean Worksheets, Userforms or Controls etc.
Normally an Object has Properties or Methods.
A Property stands for Data the Object has, a Method stands for an operation that can be ordered to the object. Properties and Methods of the Object depend on the kind of Object.
You can refer this in the VBA help. For example, a Worksheet Object has a Name Property. In the immediate window of the VBE (Visual Basic Editor), when you type
Debug.Print Activesheet.Name
it returns the name of the active worksheet. In other words, the value of the Name property is returned.
In fact, there are Blueprints to define these Properties and Methods that exist in each Object. Each Object operates according to the Blueprint.
This Blueprint is called a Class. In other words, an Object cannot exist without a Class.
When we use a word Worksheet, it generally means the Worksheet object.@Several Worksheets on the Excel workbook are managed with different respectively names.
The Worksheet is a generic name of the Worksheet object, and, in a word, it means the template of Worksheet. This template is indeed a Class.
The content of a Class doesn't exist because a Class is only a template. And Objects of an arbitrary number can be made from this blueprint.
Let's think about this using an example of the operation because it is a bit difficult at first.
 |
When you add a worksheet, you are going to make an
Instance of the Worksheet Class.
So the Icon you can see when you'd add a Worksheet can be thought as
A CLASS.@

And the added Worksheet can be thought as an Instance.
|
 |
When you add a Worksheet to a Workbook, in the back ground, a Worksheet object is made from the Blueprint of Worksheet Class, then it is added to the Workbook.
(The object made a content from the class is called an Instance. About , Ifll mention what an Instance is in the next chapter.) As a matter of fact, Worksheet, UserForm or CommandButton are names of Classes.
Character of a Class Module
What is the difference between a Class Module and other modules (a Form Module and a Standard Module)?
The role of a Standard Module is to prepare the subroutines that are shared and used by the modules in the project.
As for a Form Module, the design of the UI (user interface) and the management are the main roles.
By describing the content of actual processing separately in the standard modules, it makes the project simple, and improves the recycling of the module.
Once you write a procedure in a standard module as a public sub, you can call it from anywhere in the project.
So what is the essence of a Class Module?
A Class Module is a place where a Class is defined.
The procedure in a class module is never called directly from other modules like the procedures placed in the standard modules.
In a word, the way it is used is different. In the view of a standard module, the class module doesn't exist.
The one that exists in the view of a standard module is an instance of the object generated by the class defined by the class module and the class -individual
content.
Oppositely, a standard module is visible from any modules placed in the project. The procedure of a standard module can be called from the class module.
To be continued to the next chapter.