Object Oriented
This is a style of programming that represents concepts as objects, actions as methods and attributes as properties.
There are four core concepts when we talk about object oriented programming.
VBA does not support implementation inheritance.
VBA does support polymorphism.
VBA does support encapsulation.
Inheritance
Ability for a derived class to inherit the methods of a base class.
VBA does not support implementation inheritance.
Interface Inheritance - This is provided by the Implements keyword and lets you implement an interface differently in multiple classes.
Implementation Inheritance - Not supported in VBA. This is the ability to inherit method implementation from a base class in a derived class.
VBA does not implement derived classes, it exposes the methods that need to be implemented.
VBA does not implement interfaces, it exposes the methods that need to be implemented.
Polymorphism
Ability for a base class to implement from a derived class.
Ability for an object to decide its implementation at runtime.
If there are two classes that implement the same method you can invoke the method without knowing which class the object has been derived from.
Encapsulation
This is an implementation level process, and it is used to provide privacy and maintain control over the transparency of data at the implementation stage of a project.
Ability to keep fields, methods and properties private in a class to prevent them being accessed from outside the class.
Using Private keyword to hide variables, methods and properties inside a class.
Abstraction
This is a design level process and it is used to reduce the complexity at the designing stage
Ability to hide complexity and present a more abstract picture.
Focused on what the object does instead of how it does it.
In other languages this is achieved with interfaces and abstract classes.
class_Employee
Lets create a simple class based on an employee to illustrate how to use classes.
First you would insert a class module called "class_Employee" with the following code.
This Employee class will define a single, generic, employee.
Private field_sName As String
Private field_sDescription As String
Private field_dbHourlyRate As Double
Private field_sngHoursPerWeek As Single
Property Get Property_Name() As String
Property_Name = field_sName
End Property
Property Let Property_Name(ByVal sName As String)
field_sName = sName
End Property
Property Get Property_Department() As String
Property_Department = field_sDescription
End Property
Property Let Property_Department(ByVal sDescription As String)
field_sDescription = sDescription
End Property
Property Get Property_HourlyRate() As Double
Property_HourlyRate = field_dbHourlyRate
End Property
Property Let Property_HourlyRate(ByVal dbHourlyRate As Double)
field_dbHourlyRate = dbHourlyRate
End Property
Property Get Property_HoursPerWeek() As Single
Property_HoursPerWeek = field_sngHoursPerWeek
End Property
Property Let Property_HoursPerWeek(ByVal sngHoursPerWeek As Single)
field_sngHoursPerWeek = sngHoursPerWeek
End Property
Public Function Method_WeeklyPay() As Double
Method_WeeklyPay = (field_dbHourlyRate * field_sngHoursPerWeek)
End Function
Insert a standard module with the following code/subroutine.
To work with one particular employee, you just call the properties and methods for that employee.
Public Sub TestingClassEmployee()
Dim oEmployee As class_Employee
Set oEmployee = New class_Employee
oEmployee.Property_Name = "Richard"
oEmployee.Property_Department = "Sales"
oEmployee.Property_HourlyRate = 12.75
oEmployee.Property_HoursPerWeek = 35
Call MsgBox(oEmployee.Property_Name & " earns £" & oEmployee.Method_WeeklyPay() & " a week")
Set oEmployee = Nothing
End Sub
Circular References
This code will create a circular reference and these object references will never to destroyed.
This will create a memory leak.
Dim object1 As myClass
Dim object2 As myClass
Set object1 = New myClass
Set object2 = New myClass
object1.Link = object2
object2.Link = object1
Set object1 = New Nothing
Set object2 = New Nothing
Removing Orphaned Objects
This leaves objects in memory and eventually you will run out of memory.
You should add a class method called "Clean" to your class that cleans up any internal pointers (or references) before explicitly destroying the object references.
You should then always call this method before destroying a reference to an instance of the class.
Class - myClass
Private _objectLink As myClass
Public Get Property_Link() As myClass
Set Property_Link = _objectLink
End Sub
Public Set Property_Link(MyObject As myClass)
Set _objectLink = MyObject
End Sub
Public Sub Method_Clean()
Set _objectLink = Nothing
End Sub
Dim object1 As myClass
Dim object2 As myClass
Set object1 = New myClass
Set object2 = New myClass
object1.Link = object2
object2.Link = object1
object1.Method_Clean
object2.Method_Clean
Set object1 = New Nothing
Set object2 = New Nothing
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext