Class module General Overview

We can create a new object (beyond the built-in objects) by defining a class in the class module. The class module allows you to create new objects, along with corresponding properties, methods, and events. There are certain particular tasks that only a class module can accomplish.

  1. To handle events of objects not exposed by Excel (Workbook, Chart, QueryTable)
  2. To handle worksheet event procedure not to be contained in the sheet object module
  3. To encapsulate multiple Windows application programming interface (API) functions to execute a single procedure Class Module-Encapsulating Window API functions
  4. To enable multiple objects in a UserForm to execute a single procedure
  5. To create a custom class module

1. Instantiate the Class into an object

The class module is not an object by itself but a template of the object. It is necessary to make an object based on the class module as the very first essential step. The very first step involves a process called Instantiating to create an object. The instantiation is accomplished following the steps below.

Step Description Example
1 Start from the class module named cls_ufe, as an example The name of class module= cls_ufe
2 Declare the class module in the general declaration section of the standard module Public c_ufe as cls_ufe
3 instantiate a class into an object using New keyword Set c_ufe=New cls_ufe

2. Link the standard procedure with the class module

There are two cases: one without need to declare new object and another with need to declare new object to be linked with each other.

2.1 No need to declare new object

This case does not require additional declaration to link the standard module with the class module. The object instantiated can be used immediately.
Above Task 3 (Encapsulating multiple Windows application programming interface (API) functions to execute a single procedure) falls into this category.

2.2 Declare new object to be linked with each other

This case requires to link the object declared in standard procedure with the object to be declared in the class module using the Set keyword to assign a reference to an object. The object declared in the standard module will carry out actions using the properties and methods to be defined in the class module. If the object is a built-in object, it is possible to use the methods and properties of the object without need to define the methods and properties.

Set c_ufe . (Object_of_class_module) = (Object_of_standard_module)

3. Write a procedure in the class module named cls_ufe, for example.

3.1 No new object

The class module holds all the methods and properties without additional need to declare objects.
Above Task 3 (Encapsulating multiple Windows application programming interface (API) functions to execute a single procedure) falls into this category.

3.2 New object to be linked with each other

When the tasks with event procedures of above task 1, 2 and 4 are concerned, it is necessary to declare at the general declaration section of the class module:

Public WithEvents (Object_of_standard_module) As (Built_in_object name)


Dated on:12-NOvember-2019