This documentation is archived and is not being maintained.

Basic Class Concepts

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

In case you are not yet familiar with using class modules to build custom objects, this section covers the basics of adding a class to your project, creating an instance of a class in memory, and constructing properties and methods.

In This Section

What Is a Class?
A class is a definition for an object. It contains information about how an object should behave, including its name, methods, properties, and events.
Adding a Class Module
To add a class module to your Microsoft® Visual Basic® for Applications (VBA) project, click Class Module on the Insert menu in the Visual Basic Editor.
Creating a New Instance of a Class
To work with a custom object from code, you first create a new instance of the class that defines the object.
The Instancing Property
This property specifies whether the class module should be visible from another project when you have set a reference to the project that contains the class module.
Matters of Scope
When designing a custom object, you should think carefully about the object's scope and lifetime within the project, as well as that of its properties and methods.
Creating Simple Properties with Variables
The simplest way to create a property for a custom object is to add a public module-level variable to the class module.
Creating Methods
Public module-level variables in a class module function as properties of an object.

Related Sections

Custom Classes and Objects
If you have never used class modules to build custom objects before, this section covers the concepts that you must understand to design, build, and use custom objects with their own methods and properties.
Why Build Your Own Objects?
Build entire custom object models that involve complex code behind the scenes, but that present a relatively simple and intuitive object syntax to the programmer.
Creating Property Procedures
Public module-level variables in a class module function as properties of an object.
Creating Events and Event Procedures
Take advantage of simple to create events procedures and use ThisDocument, ThisWorkbook, or SheetN objects.
Extending Objects Through Interfaces
Suppose that in the process of designing your application, you decide that you want to create several objects that are closely related, and, in fact, require at least some of the same properties and methods.
Designing Object Models
When you design an object model, you are taking abstract processes and imposing concrete relationships upon them.
Creating Custom Objects for Web Pages
There are two ways to create objects for Web pages that are similar to custom objects created in Microsoft® Visual Basic® for Applications (VBA): by creating scriptlets, and by using Microsoft® Internet Explorer version 5 and later behaviors.