Читать книгу Excel VBA Programming For Dummies - Dick Kusleika - Страница 34

Viewing a Macro in the Visual Basic Editor

Оглавление

After you record and run a macro, you may be curious to see what the macro looks like. You might even wonder where it’s stored.

Excel stores macros in the workbook you indicate when creating the macro. So for the example used in this chapter, the macro is stored in This Workbook. To view macros, however, you need to activate the Visual Basic Editor (VBE, for short).

Follow these steps to see the macro used as an example throughout this chapter:

1 Choose Developer ⇒ Code ⇒ Visual Basic (or press Alt+F11).The Visual Basic Editor program window appears, as shown in Figure 2-4. This window is highly customizable, so your VBE window may look a bit different. The VBE window contains several other windows, which can be intimidating. Don’t fret; you’ll get used to it.

2 In the VBE window, locate the Project Explorer.The Project Explorer contains a list of all workbooks and add-ins that are currently open. Each project is arranged as a tree and can be expanded (to show more information) or contracted (to show less information). The VBE uses quite a few different windows, any of which can be open or closed. If a window isn’t immediately visible in the VBE, you can choose an option from the View menu to display the window. For instance, if the Project Explorer is not visible, you can choose View ⇒ Project Explorer (or press Ctrl+R) to display it. You can display any other VBE window in a similar manner. Chapter 3 covers the components of the VBE.

3 If necessary, expand the project that corresponds to the workbook in which you recorded the macro by clicking the plus sign next to it.If you haven’t saved the workbook, the project is probably called VBAProject (Book1).FIGURE 2-4: The VBE displays the VBA code in Module1 of Book1.

4 Click the plus sign (+) to the left of the folder named Modules.The tree expands to show Module1, which is the only module in the project.

5 Double-click Module1.The VBA code in that module is displayed in a Code pane (refer to Figure 2-4). Your screen may not look exactly the same as Figure 2-4. The code that’s recorded depends on the specific actions you made while recording the macro.

At this point, the macro probably might look a bit mysterious. Don’t worry. Travel a few chapters down the road, and all will be crystal clear.

The NameAndTime macro consists of several statements. Excel executes the statements one by one, from top to bottom. A statement that’s preceded by an apostrophe (′) is a comment. Comments are included only for your information and are ignored by Excel. In other words, Excel skips right over comments.

The first VBA statement (which begins with the word Sub) identifies the macro as a Sub procedure and gives its name; you provided this name before you started recording the macro. If you read through the code, you might be able to make sense of some of it. You see your name, the formula you entered, and lots of additional code that changes the font. The Sub procedure ends with the End Sub statement.

Excel VBA Programming For Dummies

Подняться наверх