Page 44 - Excel Progamming Weekend Crash Course
P. 44
d540629 ch02.qxd 9/2/03 9:27 AM Page 19
Session 2 — The VBA Code Editor 19
The Project Explorer
The Project Explorer, displayed in the top left corner of the VBA Editor screen, displays the
organization of the currently open VBA projects, as shown in Figure 2-1. The information is
displayed hierarchically. There is a top-level node for each open project (workbook). Under
that node are child nodes for Excel objects and — if any have been added to the project —
modules, class modules, and forms. To open the code-editing window for a component,
double-click it. To add a new module, select Insert ➪ Module. Modules are assigned default
names in the form of Module1, Module2, and so on.
Figure 2-1 Project components are listed in the Project Explorer.
There are three buttons at the top of the Project Explorer, just below its title bar. From
left to right they are:
View Code. Displays the code for the current object.
View Object. Displays the visual designer for the current object. This is applicable
only for user forms.
Toggle Folders. Toggles the Project Explorer between its regular view and folders
view. The functionality is the same either way.
Many elements on the VBA Editor screen have an associated context menu.
Right-click the element, and this menu displays a list of commands that are
Tip available for the object.
Importing and Exporting Modules
As has been mentioned previously in this session, each VBA project is stored in a single file
along with its associated workbook. Individual modules, therefore, are not stored as sepa-
rate files. You can, however, export a module to a separate file and then import that file
into another VBA project. This works for modules, class modules, user forms, and Microsoft
Excel objects. This capability enables you to share code between projects and even with
other programmers. Why write the same code more than once? If you have written the VBA
code for performing certain tasks — specific financial calculations, for example — you can
reuse it in as many projects as needed.