Introduction
One powerful feature that takes VBA programming to the next level is the Class Module.
While a User-Defined Type (UDT) is simply a container for grouping related data (variables), a Class allows you to bundle Data (Properties) and Logic (Methods) into a single, highly functional “Blueprint (Object).”
For example, if you think of a “Product” class:
- Properties (Data): Product Name, Price, Stock Quantity
- Methods (Logic):
DisplayInfo(Show details),UpdateStock(Change stock count)
By encapsulating data and related operations into one object, your code becomes much more structured, reusable, and easier to maintain.
In this article, I will explain the basic steps to create and use your own custom class in VBA.
Overview of the Steps
- Create a Class Module: Create the “blueprint” and define its properties and methods.
- Create a Standard Module: Write code to create an “instance” (actual object) of that class and use it.
Step 1: Create a Class Module
First, we create the class that serves as the blueprint for the object.
- In the VBA Editor (VBE), go to Insert > Class Module.
- In the Properties window, change the (Name) to
ClsEmployee. - Paste the following code:
Class Module: ClsEmployee
' --- Private Variables (Accessible only inside the class) ---
' Variables to hold the object's data
Private p_EmployeeID As String
Private p_FullName As String
' --- Properties (Gateways to access data from outside) ---
' EmployeeID Property (Write-only)
Public Property Let EmployeeID(value As String)
p_EmployeeID = value
End Property
' FullName Property (Write-only)
Public Property Let FullName(value As String)
p_FullName = value
End Property
' --- Methods (Actions the object can perform) ---
' Method to display information
Public Sub DisplayInfo()
MsgBox "ID: " & p_EmployeeID & vbCrLf & _
"Name: " & p_FullName, vbInformation, "Employee Info"
End Sub
Step 2: Use the Class in a Standard Module
Next, write the code in a Standard Module to actually create and use an object based on the ClsEmployee class.
Standard Module: Module1
Sub UseCustomClass()
' 1. Declare a variable using the created class type
Dim emp As ClsEmployee
' 2. Create a new object (instance) using the New keyword
Set emp = New ClsEmployee
' 3. Set values to the object's properties
emp.EmployeeID = "EMP-101"
emp.FullName = "Ichiro Sato"
' 4. Call the object's method
emp.DisplayInfo
End Sub
Execution Result
When you run the UseCustomClass macro, a message box titled “Employee Info” will appear, displaying the ID and Name you configured.
Code Explanation
Class Module (ClsEmployee)
Private p_EmployeeID As StringWe declare private variables used only inside the class. It is standard practice to make thesePrivateto protect the data from being accessed directly from the outside.Public Property Let EmployeeID(...)This defines a Property. It provides an “Entrance (Let)” for external code to assign values likeemp.EmployeeID = "...". The received value is stored in the private variablep_EmployeeID. (You can also define aGetproperty to read values).Public Sub DisplayInfo()This defines a Method. It executes a specific action using the class’s internal data.
Standard Module (Module1)
Dim emp As ClsEmployeeWe declare a variable using our custom classClsEmployeeas the data type, just likeStringorRange.Set emp = New ClsEmployeeThis line creates a specific Object (Instance) in memory from the “Blueprint” (Class). TheNewkeyword is essential here.emp.EmployeeID = "EMP-101"We use the property defined withProperty Letto set the object’s internal data.emp.DisplayInfoWe call the method defined withPublic Subto make the object perform an action.
Summary
In this article, we covered the basics of creating custom objects using Class Modules in VBA.
- Define the object’s Properties (Data) and Methods (Logic) in a Class Module.
- Generate an object (Instance) from the class using the
Newkeyword in a Standard Module. - Access the object’s properties and methods using dot notation (
.).
Mastering classes is the gateway to Object-Oriented Programming (OOP). It might feel complex at first, but it is a powerful tool for modularizing your code and significantly improving reusability and maintenance.
