[VBA] How to Use Class_Initialize (Constructor) to Set Default Values

目次

Introduction

When creating custom objects using VBA Class Modules, you often want to set default property values or run setup code the moment the object is created (instantiated).

For example, when a “Product” object is created, you might want the stock count to default to 0 automatically.

To define this “initialization process at the moment of birth,” VBA provides a special event procedure called Class_Initialize. This functions similarly to a constructor in other programming languages.

In this article, I will explain how to smartly implement class initialization using the Class_Initialize event.

VBA Sample Code Using Class_Initialize

In this example, we will create a ClsUserProfile class. When an object of this class is created, the RegisterDate property will automatically be set to the current date and time.

Step 1: Create the Class Module

  1. In the VBE, select Insert > Class Module.
  2. In the Properties window, change the (Name) to ClsUserProfile.
  3. Paste the following code:

Class Module: ClsUserProfile

' --- Properties ---
Public UserName As String
Public RegisterDate As Date

' --- Initialization Event ---
' Executed automatically when the object is created (New)
Private Sub Class_Initialize()
    ' Set default values for properties
    Me.UserName = "(Not Set)"
    Me.RegisterDate = Now() ' Set current date and time automatically
    
    ' Output to Immediate Window to confirm initialization
    Debug.Print "ClsUserProfile object has been initialized."
End Sub

' --- Method ---
' Method to return information as a string
Public Function GetInfo() As String
    GetInfo = "Name: " & Me.UserName & ", Registered: " & Me.RegisterDate
End Function

Step 2: Use the Class in a Standard Module

Next, generate an object of the ClsUserProfile class and verify that initialization occurs automatically.

Standard Module: Module1

Sub CreateUser()
    
    Debug.Print "Creating object now..."
    
    ' Create a new object (instance) using the New keyword
    ' At this moment, Class_Initialize in ClsUserProfile runs
    Dim user As New ClsUserProfile
    
    Debug.Print "Object created."
    
    ' Display default values set by the initialization event
    Debug.Print user.GetInfo()
    
    ' Set a new value to the property
    user.UserName = "John Doe"
    
    ' Display information after change
    Debug.Print user.GetInfo()

End Sub

Execution Results

When you run the CreateUser macro, the following is output to the VBE Immediate Window (Ctrl+G):

Creating object now...
ClsUserProfile object has been initialized.
Object created.
Name: (Not Set), Registered: 2025/08/17 8:41:22
Name: John Doe, Registered: 2025/08/17 8:41:22

You can confirm that Debug.Print inside Class_Initialize ran at the line New ClsUserProfile. Also, the UserName and RegisterDate held their default values immediately after creation.

Explanation of the Code

Private Sub Class_Initialize()

This is a special event procedure. You can create it by selecting Class from the left dropdown and Initialize from the right dropdown at the top of the Class Module code window.

  • The code inside this Sub runs automatically once when the object is created in memory (via Dim obj As New ... or Set obj = New ...).
  • Important: Unlike constructors in other languages, Class_Initialize cannot take arguments. You cannot pass values from the outside during initialization.
    • If you need to pass initial values, you must create a separate public method (e.g., Public Sub Init(Name As String)).

Summary

In this article, I explained how to automatically execute initialization code when an object is created using the Class_Initialize event.

  • Class_Initialize is a special event that runs automatically when an object is Newed.
  • It is perfect for setting default property values or performing setup tasks before the object is used.
  • It plays the same role as a “constructor” in other programming languages.

By utilizing Class_Initialize, you can guarantee that your objects always start in a proper state, helping you build safer and more reliable programs.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次