Microsoft Office Tutorials and References
In Depth Information
Executing Sub Procedures
You can’t have two procedures with the same name in the same module, but you can have
identically named procedures in different modules within the project. You can force VBA to execute
an ambiguously named procedure — that is, another procedure in a different module that has the
same name. To do so, precede the procedure name with the module name and a dot. For
example, say that you define procedures named MySub in Module1 and Module2 . If you want a
procedure in Module2 to call the MySub in Module1 , you can use either of the following
If you do not differentiate between procedures that have the same name, you get an
Ambiguous name detected error message.
Calling a procedure in a different workbook
In some cases, you may need your procedure to execute another procedure defined in a different
workbook. To do so, you have two options: Either establish a reference to the other workbook or
use the Run method and specify the workbook name explicitly.
To add a reference to another workbook, choose the VBE’s Tools ➜ References command. Excel
displays the References dialog box (see Figure 9-3), which lists all available references, including
all open workbooks. Simply check the box that corresponds to the workbook that you want to
add as a reference and then click OK. After you establish a reference, you can call procedures in
the workbook as if they were in the same workbook as the calling procedure.
A referenced workbook doesn’t have to be open when you create the reference; it’s treated like a
separate object library. Use the Browse button in the References dialog box to establish a
reference to a workbook that isn’t open.
Figure 9-3: The References dialog box lets you establish a reference to another workbook.