Microsoft Office Tutorials and References
In Depth Information
Executing Sub Procedures
I discuss these methods of executing procedures in the following sections.
In many cases, a procedure won’t work properly unless it’s executed in the appropriate
context. For example, if a procedure is designed to work with the active worksheet, it
will fail if a chart sheet is active. A good procedure incorporates code that checks for
the appropriate context and exits gracefully if it can’t proceed.
Executing a procedure with the Run Sub/UserForm command
The VBE Run➜Run Sub/UserForm menu command is used primarily to test a procedure while
you’re developing it. You would never require a user to activate the VBE to execute a procedure.
Choose Run➜Run Sub/UserForm in the VBE to execute the current procedure (in other words,
the procedure that contains the cursor). Or, press F5, or use the Run Sub/UserForm button on
the Standard toolbar.
If the cursor isn’t located within a procedure when you issue the Run Sub/UserForm command,
VBE displays its Macro dialog box so that you can select a procedure to execute.
Executing a procedure from the Macro dialog box
Choosing Excel’s Developer➜Code➜Macros command displays the Macro dialog box, as shown
in Figure 9-1. (You can also press Alt+F8 to access this dialog box.) Use the Macros In drop-down
box to limit the scope of the macros displayed (for example, show only the macros in the active
The Macro dialog box does not display
h Function procedures
h Sub procedures declared with the Private keyword
h Sub procedures that require one or more arguments
h Sub procedures contained in add-ins
Even though procedures stored in an add-in are not listed in the Macro dialog box, you
still can execute such a procedure if you know the name. Simply type the procedure
name in the Macro Name field in the Macro dialog box and then click Run.