The Call Statement | VBA Jump Statements

Photo by Pavan Trikutam on Unsplash
Here, you'll explore the Call statement which transfers control between procedures and DLLs. It is a VBA jump statement for non-sequential code execution.

In this article:

3 minutes read

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.

The Call statement’s syntax. Square brackets, [ ], indicate optional items.
The Call statement’s syntax. Square brackets, [ ], indicate optional items.

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.

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).
Summing two integers with the sub_SumTwoIntegersNoArgs subroutine.
Summing two integers with the sub_SumTwoIntegersNoArgs subroutine.

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Keep Learning

Related Articles

Keep Learning

Related Articles