Microsoft Office Tutorials and References
In Depth Information
Testing and Running Your Code
code to run. If the code is attached to the On Click event of a button on a
form, for example, you need to open the form in Form view and then click
the button that runs the code.
Calling a procedure from another procedure
Any VBA procedure can call another procedure, using exactly the same
syntax used to test the procedure in the Immediate window. If the sub
procedure accepts no arguments, just call the procedure by name. If the sub
procedure does contain arguments, include the passed values in the command.
You can use the Call keyword in front of the procedure name as a reminder
that you’re calling some other procedure, but the Call keyword is optional.
Figure 2-15 shows two sub procedures: one named SampleSub() and the
other named SecondSub(). The SampleSub() procedure includes a Call
statement that calls on SecondSub() to do its job. Following is what
happens when you execute SampleSub():
1. Statement1A and Statement2A in SampleSub() are executed.
2. The Call SecondSub (“Howdy World”) statement is executed,
causing Statement1B and Statement2B in SecondSub() to be executed.
3. The End Sub statement at the end of SecondSub() returns control to
the next line in the calling procedure: Statement3A .
4. Statement3A and Statement4A in SampleSub() are executed next.
5. The End Sub statement at the end of the SampleSub() procedure is
executed, and no more VBA code is executed.
Running sub procedures from macros
You can also call VBA sub procedures from macros, although technically, a
macro calls only a function procedure, not a sub procedure. You have two
choices if you still like to have a macro call a sub procedure: