Touring the Visual Basic Editor

The idea behind the Visual Basic Editor is simple: It's a separate program that's designed to do nothing else but help you create and edit VBA macros. (In professional programming circles, the Visual Basic Editor is called an integrated development environment or IDE.)

When you open the Visual Basic Editor for the first time, you don't see much. The left side of the editor has two windows labeled Project and Properties. The latter you don't need to worry about right now. (I'll talk about it in Chapter 5, "Working with Objects.") The Project window (technically, it's called the Project Explorer) shows you the contents of the current VBA project. In simplest terms, a project is an Office file and all its associated VBA items, including its macros and its user forms. (You learn about user forms in Chapter 12, "Creating Custom VBA Dialog Boxes.")

Creating a New Module

You do most of your work in the Visual Basic Editor within one or more modules, which are windows designed to hold programming code. You may already have an existing module if you recorded some macros in the previous chapter. Just in case, here are the steps to follow to create a new module:

1. In the Project Explorer on the left side of the Visual Basic Editor window, click the project into which you want to insert the new module. Here are some notes to bear in mind:

• In Word, if you want the new module's macros to be available all the time, click the Normal project (this adds the module to the Normal template).

• In Excel, if you want the new module's macros to be available all the time, click the PERSONAL.XLSB project (this adds the module to the PERSONAL workbook).

■ Remember that you won't see PERSONAL.XLSB until you store at least one recorded macro in the Personal Macro Workbook; see "Recording an Excel Macro," p. 7.

• In any program, if you want the new module's macros available only when a particular document is open, click that document or one of its objects.

2. Choose Insert, Module. The Visual Basic Editor creates the new module and opens it, as shown in Figure 2.3.

3. (Optional) In the Properties window, use the (Name) property to edit the module name, and then press Enter.

■ To learn techniques such as renaming, exporting, and deleting modules, see "Working with Modules," p.299.

Figure 2.3

A new module added to a project.

Use the (Name) property to rename the module

The new module appears in the Modules branch

The new module window appears

The new module appears in the Modules branch

The new module window appears

Figure 2.3

A new module added to a project.

Use the (Name) property to rename the module

Opening an Existing Module

If you already have an existing module in the Project window, here are the steps to follow to open it:

1. In the Project window, open a project by clicking the plus sign (+) to its left.

2. In the project you just opened, open the Modules branch by clicking the plus sign (+) to its left.

3. Double-click the name of the module you want to open. The Visual Basic Editor opens the module window and displays its VBA code.

0 0

Responses

  • VINCENZA
    How do i read exel document using visual basic editor?
    8 years ago

Post a comment