Skip to Content
Course content

8.3 Custom Classes and Objects.

In VBA, Custom Classes and Objects allow you to create your own data structures and work with them in a more structured way. By defining custom classes, you can encapsulate data and functionality into reusable components, making your code cleaner, more modular, and easier to maintain.

1. What is a Custom Class?

A Custom Class is a template for creating objects that can contain data and methods. These objects can have properties (data) and methods (functions or procedures) that operate on the data. The purpose of creating custom classes is to model real-world objects or abstract concepts within your code.

Key Concepts of Classes and Objects:

  • Class: A blueprint or template that defines the structure and behavior of an object.
  • Object: An instance of a class. When you create an object, you are creating an instance of the class, which can have its own data and behavior.
  • Properties: The data stored within an object.
  • Methods: The actions or functions that an object can perform.
  • Events: Actions or triggers that occur in response to changes in the object.

2. Creating a Custom Class

To create a custom class in VBA:

  1. Insert a Class Module:
    • In the VBA editor, click Insert > Class Module. This creates a new class module.
    • By default, the class is named Class1, but you can rename it to something more meaningful.
  2. Define Properties and Methods:
    • In the class module, you can define properties (variables) and methods (functions or subroutines).

Example: Creating a Car Class

' Class Module: Car
Private pMake As String
Private pModel As String
Private pYear As Integer

' Property for Make
Public Property Get Make() As String
    Make = pMake
End Property
Public Property Let Make(Value As String)
    pMake = Value
End Property

' Property for Model
Public Property Get Model() As String
    Model = pModel
End Property
Public Property Let Model(Value As String)
    pModel = Value
End Property

' Property for Year
Public Property Get Year() As Integer
    Year = pYear
End Property
Public Property Let Year(Value As Integer)
    pYear = Value
End Property

' Method to display car details
Public Sub DisplayDetails()
    MsgBox "Make: " & pMake & vbCrLf & "Model: " & pModel & vbCrLf & "Year: " & pYear
End Sub

In the example above:

  • The Car class has three properties: Make, Model, and Year.
  • Each property has a Get and Let procedure for reading and setting the values.
  • The DisplayDetails method shows the details of the car in a message box.

3. Creating and Using Objects

Once you've defined a class, you can create objects from that class in a regular module or another class.

Example: Using the Car Class in a Module

Sub CreateCarObject()
    ' Declare an object variable
    Dim myCar As Car
    
    ' Create an instance of the Car class
    Set myCar = New Car
    
    ' Set properties using the Let property procedures
    myCar.Make = "Toyota"
    myCar.Model = "Camry"
    myCar.Year = 2020
    
    ' Call the method to display car details
    myCar.DisplayDetails
End Sub

In this example:

  • An object myCar of type Car is created.
  • The properties Make, Model, and Year are set.
  • The DisplayDetails method is called to show the car's information.

4. Object-Oriented Principles in VBA

VBA allows you to apply basic Object-Oriented Programming (OOP) principles, such as:

  • Encapsulation: Hiding the internal state of an object and requiring all interactions to be done through defined properties and methods.
  • Abstraction: Simplifying complex systems by modeling only relevant properties and behaviors.
  • Reusability: Once a class is defined, you can create multiple objects of that class with different data.

Example of Encapsulation and Abstraction

' Class Module: Rectangle
Private pLength As Double
Private pWidth As Double

' Property for Length
Public Property Get Length() As Double
    Length = pLength
End Property
Public Property Let Length(Value As Double)
    pLength = Value
End Property

' Property for Width
Public Property Get Width() As Double
    Width = pWidth
End Property
Public Property Let Width(Value As Double)
    pWidth = Value
End Property

' Method to calculate the area
Public Function Area() As Double
    Area = pLength * pWidth
End Function

Here, the Rectangle class has properties for Length and Width and a method Area to calculate the area of the rectangle. This encapsulates the logic for calculating the area, and users interact with the class only via its properties and methods, not the internal variables.

5. Advantages of Using Custom Classes

  1. Organization: Custom classes allow you to organize related data and functionality into a single, logical unit.
  2. Modularity: You can create classes to represent different objects in your application, making your code more modular and easier to maintain.
  3. Reuse: Once a class is created, you can reuse it multiple times to create different objects with varying properties.
  4. Encapsulation: Custom classes encapsulate data and functionality, making your code easier to understand and debug.

6. Summary of Custom Classes and Objects

  • Class: A template or blueprint for creating objects.
  • Object: An instance of a class.
  • Properties: Variables that store data for an object.
  • Methods: Procedures or functions that perform actions on an object.
  • Encapsulation: Hides the internal workings of a class from outside code.

By creating custom classes and objects, you can implement more sophisticated data structures, improve code organization, and follow object-oriented programming principles in your VBA applications.

Commenting is not enabled on this course.