Microsoft Office Tutorials and References
In Depth Information
Testing and Running Your Code
✦ Convert the sub procedure to a function procedure.
✦ Write a function procedure that calls the sub procedure and then call
the function procedure from the macro.
Converting a sub procedure to a function procedure is a simple matter of
changing the Sub keyword at the top of the procedure to Function and the
End Sub statement at the bottom of the procedure to End Function.
If you want to leave the procedure as is and call it from a function, place the
call to the sub procedure within a function procedure. When executed, the
following DoMySub() function procedure calls the MySub() sub procedure:
‘The sub procedure below is named MySub()
MsgBox (“MySub Ran”)
‘Function procedure below calls MySub sub procedure.
To run MySub from a macro, choose RunCode as the macro action. Then
type the name of the function procedure — in this example, DoMySub() —
as the action argument for the RunCode action, as shown in Figure 2-16.
For details on defining macro actions, see Book VI, Chapter 1.
Testing function procedures
Unlike sub procedures, which return no value, function procedures always
return a value. To test a function from the Immediate window, use the ?
“What is . . . ?” ) symbol followed by the function name — and if necessary, (
use the values that pass to the function.