Table Of Contents
Excel VBA Class Modules
When we use VBA we use the properties and attributes defined in VBA but what happens when we want to create our own properties and methods and attributes, that is when we use a class module in VBA so that we can have it user-defined, a class module has its own set of codes defined for functions, properties, and objects by the user.
Class Modules are used to create an object. When we say items, even though it is a variable, those are small programs. While writing the code, we usually write in modules. Essential modules are where we write our principles to do the job. We also use User Form to create Graphic User Interfaces.
But if you look at the above image, you can see “Class Module.” I know for sure you have not touched that until you are reading this post. You must be wondering what is this VBA class module is when all the job can be done by using our regular Module itself.
What is the Class Module?
Class Modules allow a user to create their object just like how we have built-in items in standard modules like “Worksheets,” “Workbooks,” “Range,” and so on.
Like this using a class module, we can create custom objects.
The class has a direct relationship with objects. For example, you have a machine diagram to build a machine but remember it is not a machine yet and using this machine diagram, and we can make many machines like that.
For example, if you want to list various machine brands to list the characteristics of multiple models.
In the machine, we have a brand name, series number, machine power, color of the device, number of motors involved in it, motor fuel type, etc.… In technical language, these are called “properties.”
Concerning the properties of the machine, we can start, we can turn off, we can increase the speed of the motor, we can pause, etc... And these are called “Methods.”
Example
Let’s start the ball rolling because it is always the boring thing to read out the theoretical part. To insert a class module, go to the insert option in a basic visual editor.
Now we can see a class module like the below.
This looks similar to the one we have above as a regular module. Change the name of the class module in the properties window. To see the properties window, press the F4 key.
Now declare the variable as a string.
Without creating a subprocedure in VBA, we need to declare the variable and use the word “Public” not “Dim.”
Now we can access this variable in any module and class module.
Now go to a regular module and name the variable.
After declaring the variable, we need to assign the data type in VBA; instead of setting the data type, we can give the name of the class module, i.e., CM.
Using the variable “k,” we can access the public variable we have defined in the class module, i.e., “My Value.”
As we can see in the above picture, it is showing the option of variable name from the class module to assign the value to it.
Now show the value of the assigned variable in the VBA message box.
Code:
Sub Class_Example() Dim k As New CM k.MyValue = "Hello" MsgBox k.MyValue End Sub
Run this code using the F5 key or manually to show the result.
Class Module vs. Objects
At the initial stage of the class module, everybody gets confused with what class is and what an object is.
To understand this, recollect our earlier example of a machine diagram. The first thing we need to produce a machine is we need to design the machine first, and then several copies can be replicated with that design.
Now relate this to our class module.
- Here Class Module is a Design. And Object is the copy created by the Design.
- One more interesting thing is we need to use the word “new” to create an object from the class module.
Below is an example of the same.
One more thing when we use built-in objects like worksheets, workbooks, and range objects, we don’t use the word “new.”
To start off the proceedings with the Class Module, these basic things you should know. In the coming articles, we will see the next level examples.
It seems difficult to understand this; the more time you spend with a class module, you will get used to it.