Microsoft Office Tutorials and References
In Depth Information
Executing Sub Procedures
Executing a procedure from another procedure
One of the most common ways to execute a procedure is from another VBA procedure. You have
three ways to do this:
h Enter the procedure’s name, followed by its arguments (if any) separated by commas.
h Use the Call keyword followed by the procedure’s name and then its arguments (if any)
enclosed in parentheses and separated by commas.
h Use the Run method of the Application object. The Run method is useful when you
need to run a procedure whose name is assigned to a variable. You can then pass the
variable as an argument to the Run method.
The following example demonstrates the first method. In this case, the MySub procedure
processes some statements (not shown), executes the UpdateSheet procedure, and then executes
the rest of the statements.
Sub MySub()
‘ ... [code goes here] ...
‘ ... [code goes here] ...
End Sub
Sub UpdateSheet()
‘ ... [code goes here] ...
End Sub
The following example demonstrates the second method. The Call keyword executes the
Update procedure, which requires one argument; the calling procedure passes the argument to
the called procedure. I discuss procedure arguments later in this chapter (see “Passing
Arguments to Procedures”).
Sub MySub()
MonthNum = InputBox(“Enter the month number: “)
Call UpdateSheet(MonthNum)
‘ ... [code goes here] ...
End Sub
Sub UpdateSheet(MonthSeq)
‘ ... [code goes here] ...
End Sub
Even though it’s optional, some programmers always use the Call keyword just to
make it perfectly clear that another procedure is being called.
Search JabSto ::

Custom Search