33

There's some code in our project that looks a bit like this:

Private Sub Method1()
    Call InnerMethod
End Sub

Private Sub Method2()
    InnerMethod
End Sub

Private Sub InnerMethod()
    '' stuff
End Sub

What's the advantage of doing Method1 over Method2?

5 Answers 5

37

From the MSDN:

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.

For example:

Sub Proc1()
    Debug.Print "Hello World"
End Sub

Sub Proc2(text As String)
    Debug.Print "Hello " & text
End Sub

In the immediate window, if you enter

Proc1

then "Hello World" prints. If you enter

Call Proc1

then "Hello World" prints. If you enter

Proc2 "World"

then "Hello World" prints. If you enter

Call Proc2 "World" 

you get a compile error. You would have to enter

Call Proc2("World")
11

Call does nothing special other than call the method. It is a hang over from the old days of Basic when all lines had to start with a keyword. "Let" is another of these keywords, which was always put before an assignment, but is no longer required.

Method1 and Method2 do the exact same thing.

4

I have found a major difference about 'call' keyword with functions that having, ByRef Arguments (I have found this in MS-Access VBA editor). If you are calling the function without 'Call' keyword, ByRef aruments will not set for the calle. For Ex:

Private Function Test(Optional ByRef refArg As String) As Boolean    
    refArg = "Test"
    Test = True
End Function

If you call the function without the Call keyword like

Dim a As String
Test(a)

a will be an empty string, after the call returns

If you call the function with the Call keyword like

Dim a As String
Call Test(a)

a will contain the string Test

The detailed explanation provided in the following link: Cannot use parentheses when calling a Sub

3
  • 5
    The two are not equivalent. In the first case, enclosing the argument in parens causes it to be evaluated, and the result of the evaluation passed: in essence, you're passing a copy of "a", not "a" itself. This is made obvious by the parser. When you enter Test(a) and leave the line, the parser will add a space, making it Test (a) to indicate that it's an expression; whereas with Call Test(a) no space will be added -- you're passing "a" directly. See the answer by Patrick Cuff above for the doc reference.
    – Jim Mack
    Jul 1, 2010 at 20:21
  • Indeed, and actually getting the result (for example, MsgBox Test(a)) restores the equivalence.
    – Ant
    Jul 5, 2010 at 10:21
  • You should delete this answer. It is extremely misleading... and wrong. Down-voted. Never call a procedure with parentheses around the argument list... with two exceptions: using the Call keyword or doing an assignment from the result of a function. You are not doing either and so the variable surrounded by parentheses gets evaluated and sent to the procedure as a literal value (not the variable) and since it is no longer a variable, there is no way to get the updated value back. You don't need Call in your example... you just need to remove the parentheses from around the argument: Test a
    – Excel Hero
    Dec 31, 2020 at 23:47
2

There's no difference.

2

Here's a post which describes when you need to use call vs not using it and when to parentheses around your parameters.

You can also read more about call from MSDN. Essentially the main difference is that when you use call to call a function you can't access the return value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.