Matters of Scope

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.

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. The following sections outline some things to consider when deciding how to scope object variables and procedures.

In This Section

  • Object Variable Scope
    When you create a new instance of a class and assign it to an object variable, that instance exists in memory until the object variable goes out of scope, or until you explicitly set it to Nothing in your code.
  • Member Scope
    The set of public variables, methods, properties, and events described in a class module define the interface for an object.
  • Basic Class Concepts
    Become familiar with using class modules to build custom objects, and learn the basics of adding a class to your project, creating an instance of a class in memory, and constructing properties and methods.
  • 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.
  • 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.