Finance

Charts

Statistics

Macros

Search

Creating Classes, Objects, and Collections with Excel VBA

In VBA, along with a large number of built-in objects (Application, Workbook, Worksheet, Range, etc.), there is also the possibility of creating user-defined objects.

The use of user-defined objects allows programs to be shorter, more transparent, and easier to understand. User-defined objects in VBA have properties, fields, methods, and can respond to events. Unfortunately, however, they do not support the inheritance mechanism typical of fully object-oriented languages.

Objects are instances (representatives) of classes. A method that an object executes in response to a message is determined by the class to which the receiving object belongs. All objects of the same class use the same methods in response to the same messages.

The members of a class are fields, properties, methods, and events. Let us consider them in more detail:

  • Field — a variable that holds a certain value. Thus, a field provides information about the object.
    Example: if there is an object Car, it may have a field Cylinders that stores the number of cylinders in the car’s engine.
  • Method — code that programs an action the object must perform.
    Example: the object Car could be repainted using the method Repaint.
  • Property — serves the same role as a field (providing information about the object), but is created using special Property procedures that offer more flexibility in both assigning and retrieving values. A property allows data to be separated from the object, making them more robust.
  • Event — allows objects to notify one another about a change in circumstances. Events are often used in graphical interfaces to notify, for example, when a user clicks a button. But they are also suitable for other kinds of notifications, such as receiving an email message.
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx