VBA Call Sub

What is Excel VBA Call Sub?

In VBA (Visual Basic for Applications), “Call Sub” is a statement used to call a subroutine or a procedure within a VBA module. A subroutine is a block of code that performs a specific task or a series of tasks. It can be defined within a VBA module and called from other parts of the code using the “Call Sub” statement or simply utilizing the subroutine name followed by parentheses.

Consider the following example.

VBA Sub Call - Intro

Here, we write a subroutine to print a text and call it from the function, CallSub(), to get the output printed.

VBA Sub Call - Intro Output

Note: In newer versions of VBA, starting with VBA 6 (used in Office 2000 and later versions), the “Call” keyword is optional. So, you can omit it and directly call the subroutine.

Key Takeaways
  • The Call Sub is used to call a sub-procedure from within a VBA module.
  • To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses.
  • The Call Sub does not return a value, unlike a function.
  • The Call Sub does not have a Return statement. A statement used in a Sub procedure passes values to a called procedure using named arguments with a colon and equal to symbol.

How to Call Subroutine in Excel VBA?

To implement VBA Call Subroutine in Excel VBA, we perform the following steps as shown below:

  1. Go to the “Developer” section in the toolbar and click the “Visual Basic” option. Now, the VBA Editor opens to add functions and Sub procedures. Then, click the “Insert” and “Module” buttons to create a new module or blank page.

    Developer - Visual Basic

    Insert - Module

  2. Initialize a sub-procedure to perform addition on a given variable by 1.

    VBA Sub Call - Step 2

  3. Accept an Integer input variable.


    VBA Sub Call - Step 3

  4. Add one to the integer variable and print it.


    VBA Sub Call - Step 4

  5. Initialize another subroutine to perform VBA Call Sub.


    VBA Sub Call - Step 5

  6. Perform VBA Call Sub.


    VBA Sub Call - Step 6

    Code:
    Sub AddOne()
    num = 10
    num = num + 1
    Debug.Print num


    End Sub
    Sub CallS()
    AddOne
    End Sub

  7. Print the number by pressing the F5 or Run buttons on the VBA toolbar.


    VBA Sub Call - Step 7

    It prints 11 since we declared num as 10, and we add 1 to it as defined in the function. Hence, the output is shown as 11.


Excel VBA – All in One Courses Bundle (35+ Hours of Video Tutorials)

If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

Examples

Let us look at a few examples of implementing the VBA Sub Call procedure.

Example #1 – VBA Call Sub from another module

Consider an example where we must implement a VBA Call Sub from another sheet by adding three numbers.

Step 1: In the “Developer” tab in the toolbar, click the “Visual Basic” option and open a new module. Define a sub-procedure that adds three numbers and prints the output.

VBA Sub Call - Example 1 - Step 1

Step 2: Define an integer that will hold the sum value of all three numbers.

VBA Sub Call - Example 1 - Step 2

Step 3: Add all three variables and assign the sum to the result variable declared in the earlier steps.

VBA Sub Call - Example 1 - Step 3

Step 4: Print the result variable.

VBA Sub Call - Example 1 - Step 4

Step 5: Click the “Insert” and “Module” buttons to create a new module or blank page.

VBA Sub Call - Example 1 - Step 5.jpg

Step 6: Create a new subroutine in the new module that performs VBA Call Sub with variable.

VBA Sub Call - Example 1 - Step 6

Step 7: Declare three integer variables to be added.

VBA Sub Call - Example 1 - Step 7

Step 8: Get the numbers from the user.

VBA Sub Call - Example 1 - Step 8

Step 9: Now, perform the Call Sub from another sheet by calling the Module name along with the subroutine.

VBA Sub Call - Example 1 - Step 9

It performs VBA Call Sub with parameters. Here, we can call subroutines from a module to any other modules.

Code:

Sub SumOfThreeNumbers(ByRef n1 As Integer, ByRef n2 As Integer, ByRef n3 As Integer)
Dim res As Integer
res = n1 + n2 + n3
Debug.Print “The sum is: ” & res
End Sub

In another module:

Sub GetThreeNumbers()
Dim n1 As Integer
Dim n2 As Integer
Dim n3 As Integer
n1 = InputBox(“Enter a number”)
n2 = InputBox(“Enter a number”)
n3 = InputBox(“Enter a number”)
Module1.SumOfThreeNumbers n1, n2, n3
End Sub

Step 10: Run the VBA code in the second module. The output is printed as shown:

VBA Sub Call - Example 1 - Step 10 - 22.jpg
VBA Sub Call - Example 1 - Step 10 - 44
VBA Sub Call - Example 1 - Step 10 - 66

The sum will be printed in the Immediate tab as shown.

VBA Sub Call - Example 1 - Step 10 - Output

Example #2 – VBA Call Sub with multiple arguments

Consider an example where you need to VBA Call Sub with multiple arguments. There may be more than one argument in a subroutine. You must create subroutines that implement the AND Gate function with three inputs.

Step 1: In a new module, initialize a sub-procedure to mimic Boolean AND Gate.

VBA Sub Call - Example 2 - Step 1

Step 2: Declare a result variable as Boolean.

VBA Sub Call - Example 2 - Step 2

Step 3: Initialize an If-Else condition to check whether all three variable values are “True.”

VBA Sub Call - Example 2 - Step 3

If all three of them are “True, ” the result is also returned as True. If not, the result value is returned as “False.”

Step 4: Print the result.

Example 2 - Step 4

Step 5: Declare a subroutine that accepts Boolean input arguments as input from the user and performs VBA Call Sub with parameters.

Example 2 - Step 5

Step 6: Declare three variables as the Boolean datatype.

Example 2 - Step 6

Step 7: Accept input of all three variables.

Example 2 - Step 7

Step 8: VBA Sub Call with arguments for the And Gate subroutine.

Example 2 - Step 8

Code:

Sub AndGate(x As Boolean, y As Boolean, z As Boolean)
Dim res As Boolean
If (x = True And y = True And z = True) Then
res = True
Else: res = False
End If
Debug.Print “The result is: ” & res
End Sub

Sub InputsForGate()
Dim x As Boolean
Dim y As Boolean
Dim z As Boolean
x = InputBox(“True or False”)
y = InputBox(“True or False”)
z = InputBox(“True or False”)
AndGate x, y, z
End Sub

Step 9: Run the above code.

VBA Sub Call - Example 2 - Step 9
VBA Sub Call - Example 2 - Step 9
VBA Sub Call - Example 2 - Step 9

This will result in “True” being printed in the immediate tab.

VBA Sub Call - Example 2 - Output

Example #3

Consider an example where you need to perform string concatenation using VBA Sub Call with variables. Here we accept two string values and concatenate both string values into one consolidated string.

Step 1: Initialize a sub-procedure to concatenate two strings.

Example 3 - Step 1

Step 2: Declare a variable, result, as a string to hold the value of the concatenated string values.

Example 3 - Step 2

Step 3: Print the concatenated string.

Example 3 - Step 3

Step 4: Declare the subroutine to call the string concatenation subroutine.

Example 3 - Step 4

Step 5: Declare two strings and initialize their values.

Example 3 - Step 5

Step 6: VBA Sub Call with Argument the concatenation subroutine.

Example 3 - Step 6.jpg

Code:

Sub ConcatenateStrings(s1 As String, s2 As String)
Dim result As String
result = s1 & s2
Debug.Print “The concatenated strings are: ” & result
End Sub

Sub CallString()
Dim string1 As String
Dim string2 As String
string1 = “Hello”
string2 = “World!”
ConcatenateStrings string1, string2
End Sub

Step 7: The output is as shown:

VBA Sub Call - Example 3 - Step 7

Important Things To Note

  • Pass arguments to subroutines as needed. Arguments allow you to pass values or objects to subroutines, enabling them to perform tasks based on specific data.
  • Ensure that the argument values or variables match the called subroutine’s expected parameters.
  • Don’t use the “Call” keyword unnecessarily. In newer versions of VBA, starting with VBA 6, the “Call” keyword is optional, and you can directly call a subroutine without it.
  • Don’t use subroutines for tasks that can be better accomplished using functions. Functions can return values, allowing you to use the result directly in expressions.

Frequently Asked Questions (FAQs)

1. Can you call a private sub in VBA?

No, you cannot call a private subroutine directly from outside the module it is defined in.

2. Can you call a sub within function VBA?

No, you cannot directly call a subroutine within a function in VBA.

3. What is the difference between Call Sub and function in VBA?

VBA Sub Call - FAQ 3

4. Why is Call Sub in VBA not working?

Incorrect subroutine name or syntax: Double-check that you are using the correct subroutine name and that the syntax is accurate.
Subroutine not defined: Ensure that the subroutine you are trying to call is defined in the same module or in a referenced module.
The subroutine is private: If the subroutine is declared private, it cannot be called from outside the module where it is defined.
Subroutine not in scope: Make sure that the subroutine is accessible from the location where you are trying to call it. Check for any scoping issues or modules that need to be referenced.
Compilation errors: Check for any compilation errors in your VBA project that may prevent the subroutine from being called.

This has been a guide to VBA Call Sub. Here we learn how to call subroutine or procedure within a VBA Module with examples & downloadable template. You can learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *