Chapter 3. The Visual Basic Editor, Part I

The first step in becoming an Excel VBA programmer is to become familiar with the environment in which Excel VBA programming is done. Each of the main Office applications has a programming environment referred to as its Integrated Development Environment (IDE). Microsoft also refers to this programming environment as the Visual Basic Editor.

Our plan in this chapter and Chapter 4 is to describe the major components of the Excel IDE. We realize that you are probably anxious to get to some actual programming, but it is necessary to gain some familiarity with the IDE before you can use it. Nevertheless, you may want to read quickly through this chapter and the next and then refer back to them as needed.

In Office 97, the Word, Excel, and PowerPoint IDEs have the same appearance, shown in Figure 3-1. (Beginning with Office 2000, Microsoft Access also uses this IDE.) To start the Excel IDE, simply choose Visual Basic Editor from the Macros submenu of the Tools menu, or hit Alt-F11.

The Excel VBA IDE

Figure 3-1. The Excel VBA IDE

Let us take a look at some of the components of this IDE.

The window in the upper-left corner of the client area (below the toolbar) is called the Project Explorer. Figure 3-2 shows a close-up of this window.

Note that the Project Explorer has a treelike structure, similar to the Windows Explorer's folders pane (the left-hand pane). Each entry in the Project Explorer is called a node . The top nodes, of which there are two in Figure 3-2, represent the currently open Excel VBA projects (hence the name Project Explorer). The view of each project can be expanded or contracted by clicking on the small boxes (just as with Windows Explorer). Note that there is one project for each currently open Excel workbook.

At the level immediately below the top (project) level, as Figure 3-2 shows, there are nodes named:

Microsoft Excel Objects
Forms
Modules
Classes

Under the Microsoft Excel Objects node, there is a node for each worksheet and chartsheet in the workbook, as well as a special node called ThisWorkbook, which represents the workbook itself. These nodes provide access to the code windows for each of these objects, where we can write our code.

Under the Forms node, there is a node for each form in the project. Forms are also called UserForms or custom dialog boxes. We will discuss UserForms later in this chapter.

Under the Modules node, there is a node for each code module in the project. Code modules are also called standard modules. We will discuss modules later in this chapter.

Under the Classes node, there is a node for each class module in the project. We will discuss classes later in this chapter.

The main purpose of the Project Explorer is to allow us to navigate around the project. Worksheets and UserForms have two components—a visible component (a worksheet or dialog) and a code component. By right-clicking on a worksheet or UserForm node, we can choose to view the object itself or the code component for that object. Standard modules and class modules have only a code component, which we can view by double-clicking on the corresponding node.

Let us take a closer look at the various components of an Excel project.

As you no doubt know, Excel contains a great many built-in dialog boxes. It is also possible to create custom dialog boxes, also called forms or UserForms. This is done by creating UserForm objects. Figure 3-3 shows the design environment for the Select Special UserForm that we mentioned in Chapter 1.

The large window on the upper-center in Figure 3-3 contains the custom dialog box (named dlgSelectSpecial) in its design mode. There is a floating Toolbox window on the right that contains icons for various Windows controls.

To place a control on the dialog box, simply click on the icon in the Toolbox and then drag and size a rectangle on the dialog box. This rectangle is replaced by the control of the same size as the rectangle. The properties of the UserForm object or of any controls on the form can be changed by selecting the object and making the changes in the Properties window, which we discuss in the next section.

In addition to the form itself and its controls, a UserForm object contains code that the VBA programmer writes in support of these objects. For instance, a command button has a Click event that fires when the user clicks on the button. If we place such a button on the form, then we must write the code that is run when the Click event fires; otherwise, clicking the button does nothing. For instance, the following is the code for the Close button's Click event in Figure 3-3. Note that the Name property of the command button has been set to cmdClose :

Private Sub cmdClose_Click()
   Unload Me
End Sub

All this code does is unload the form.

Along with event code for a form and its controls, we can also include support procedures within the UserForm object.

Don't worry if all this seems rather vague now. We will devote an entire chapter to creating custom dialog boxes (that is, UserForm objects) later in the book and see several real-life examples throughout the book.