Читать книгу Excel Macros For Dummies - Dick Kusleika - Страница 41
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 choose to store the recorded macro, just before you start 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 module-level variables (variables that apply to all procedures in the module instead of just one), or set some other module-wide options.
Sub procedures: A set of programming instructions that performs some action. All recorded macros are 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 dependent on how may macros you have. If you have just a few, one module is probably all you need. If you start to get a lot of macros in a workbook, splitting them into well-named folders is best so you can easily find them later. Simply cut and paste any code from one module to another to move it.
Follow these steps to manually add a new VBA module to a project:
1 Select the project’s name in the Project Explorer.
2 Choose Insert ⇒ Module.
Or you can
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 Explorer (see Figure 2-3). Any modules you create in a given workbook are placed in this Modules folder.
FIGURE 2-3: Code modules are visible in the Project Explorer in a folder called Modules.
If you want to change the name of your module, select the module in the Project Explorer and press F4 to show the Properties window. Modules only have one property, Name, so it's easy to find where to change it.