VBA programs execute code-lines in a sequential manner. However, situations often arise where you need to skip ahead or circle back to a code-line. That is where jump statements like the Call statement come into play. They are of immense importance as they allow the non-sequential transfer of control (i.e., program execution).
Introducing the Call statement
The Call statement acts counter to the Exit statement for Sub and Function procedures and dynamic-link libraries. Rather than leaving them, it instantly transfers control to them. Moreover, the Exit statement doesn’t work for dynamic-link libraries.
The caller procedure is the procedure that houses the Call statement. Whereas the procedure to which the statement sends control is the called procedure.
Like the Exit statement, control returns to the code-line right after the Call statement in the caller procedure. Of course, that happens after the called procedure finishes its run.
Salient Points on Usage
There are several vital things to note about the Call statement:
- The Call keyword is optional. But you must parenthesize the argument list when you use it. Otherwise, if you omit it then you must also omit the argument list’s parentheses.
- Using the statement on a Function procedure discards its return value after it runs. If you need a function’s return value, use the assignment operator (=) as described here instead.
- The argument list can’t include the ByVal or ByRef keywords. So, unless you are calling a dynamic-link library, don’t use these keywords.
- You can pass entire arrays into the called procedure. However, the array name must be followed by empty parentheses to do so.
The Call Statement in Action
The sample code below shows the statement in use in summing two numbers. It also illustrates the salient points discussed above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Option Explicit Private Sub sub_SumTwoIntegersNoArgs() 'declare procedure-level variables Dim int_Number1 As Integer, int_Number2 As Integer, int_Sum As Integer 'get the two integers from the User int_Number1 = Val(InputBox("Enter Number1:")) int_Number2 = Val(InputBox("Enter Number2:")) 'compute sum and display result in a Message Box int_Sum = int_Number1 + int_Number2 MsgBox "sub_SumTwoIntegersNoArgs = " + CStr(int_Sum), vbOKOnly End Sub Public Sub RunSumSubroutines() 'sum integers provided by User Call sub_SumTwoIntegersNoArgs End Sub |
In brief, the program does the following:
- Declares subroutine, sub_SumTwoIntegersNoArgs, without arguments (code window lines #4 and #17).
- Declares three integer variables, int_Number1, int_Number2, and int_Sum (line #7). These will store the numbers to add and the result obtained, respectively.
- The subroutine gets the two integers to be summed from the user via the InputBox function (lines #10-11). It stores the numbers in variables int_Number1 and int_Number2.
- The subroutine computes the sum and stores the result in int_Sum (line #14).
- Finally, the subroutine displays the result using the MsgBox function (line #15).
The image above shows the workings of the sub_SumTwoIntegersNoArgs subroutine.
A second Sub procedure, sub_RunSum, calls (i.e., invokes) sub_SumTwoIntegersNoArgs (line #23). sub_RunSum is declared without arguments (lines #20 and #25), and it:
- Transfers execution to sub_SumTwoIntegersNoArgs via the Call statement (line #23).
- Execution returns to the line following the Call statement once sub_SumTwoIntegersNoArgs has run.