Custom Collections and Class Modules

Working with Collections ■ Declaring a Custom Collection ■ Adding Objects to a Custom Collection ■ Removing Objects from a Custom Collection ■ Insert: Module or Class Module? ■ Creating Custom Objects ■ Creating a Class ■ Variable Declarations ■ Defining the Properties for the Class ■ Creating the Property Get Procedures ■ Creating the Property Let Procedures ■ Creating the Class Methods ■ Creating an Instance of a Class ■ Event Procedures in the Class Module ■ Creating the User Interface ■ Watching the Execution of Your VBA Procedures ■ What's Next...

In Chapter 9, you learned how to control the objects of another application by using Automation. Recall that after setting up a reference to the Microsoft Word 10.0 Object Library, you were able to control the Word application remotely by calling upon its own objects, properties, and methods. You've also learned how to use Automation to retrieve a list of contacts from Microsoft Outlook to an Excel spreadsheet. The good news is that you are not limited to using objects built into Microsoft Excel or objects exposed by other applications. VBA allows you to create your own objects and collections of objects, complete with their own methods and properties. In this chapter, you will learn how to work with collections, including how to declare a custom Collection object. You will also learn how to use class modules to create user-defined objects.

Before diving into the theory and hands-on examples in this chapter, let's start by going over several terms that will be used throughout this chapter:

Collection—An object that contains a set of related objects. Class—A definition of an object that includes its name, properties, methods, and events. The class acts as a sort of object template from which an instance of an object is created at run time. Instance—A specific object that belongs to a class is referred to as an instance of the class. When you create an instance, you create a new object that has the properties and methods defined by the class. Class Module—A module that contains the definition of a class, including its property and method definitions. Module—A module containing Sub and Function procedures that are available to other VBA procedures and are not related to any object in particular.

Form Module—A module that contains the VBA code for all event procedures triggered by events occurring in a user form or its controls. A form module is a type of class module. Event—An action recognized by an object, such as a mouse click or a keypress, for which you can define a response. Events can be caused by a user action or a VBA statement or can be triggered by the system. Event Procedure—A procedure that is automatically executed in response to an event initiated by the user or program code or triggered by the system.

0 0

Post a comment