Читать книгу Excel 2019 Power Programming with VBA - Michael Alexander, Dick Kusleika - Страница 93

Adding a new VBA module

Оглавление

When you record a macro, Excel automatically inserts a VBA module to hold the recorded code. The workbook that holds the module for the recorded macro depends on where you chose to store the recorded macro, just before you started recording.

In general, a VBA module can hold three types of code.

 Declarations One or more information statements that you provide to VBA. For example, you can declare the data type for variables you plan to use or set some other module-wide options.

 Sub procedures A set of programming instructions that performs some action. All recorded macros will be Sub procedures.

 Function procedures A set of programming instructions that returns a single value (similar in concept to a worksheet function, such as Sum).

A single VBA module can store any number of Sub procedures, Function procedures, and declarations. How you organize a VBA module is completely up to you. Some people prefer to keep all of their VBA code for an application in a single VBA module; others like to split up the code into several different modules. It's a personal choice, just like arranging furniture.

Follow these steps to add a new VBA module manually to a project:

1 Select the project's name in the Project window.

2 Choose Insert ➪ Module.

Or you can do the following:

1 Right-click the project's name.

2 Choose Insert ➪ Module from the shortcut menu.

The new module is added to a Modules folder in the Project window (see Figure 2.12). Any module that you create in a given workbook is placed in this Modules folder.


FIGURE 2.12 Code modules are visible in the Project window in a folder called Modules.

Excel 2019 Power Programming with VBA

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