1

I just figured out that setting optional parameters requires "Call" infront of the method.

Public Sub Test()

    Call abc("aaa")
    Call abc("aaa", 2)

    abc("aaa") ' is fine
    abc("aaa", 2) ' is a syntax error

End Sub


Function abc(a As String, Optional iCol As Long = 3)

    MsgBox (iCol)

End Function

Can you add a "why does this make sense?" to my new information?

Greetings, Peter

Edit: PS the function abc for no other use than to simplify the question.

2
  • There are some nice examples and explanations about when Call can and can't be used below but the fact is: there is virtually no reason to use it -- with one small exception. So just avoid until completely necessary. Commented Apr 7, 2017 at 13:46
  • The accepted answer skirts your question. YOU DO NOT need to use "Call" to set optional parameters. Commented Mar 13, 2019 at 22:06

3 Answers 3

2

Documentation

Call is an optional keyword, but the one caveat is that if you use it you must include the parentheses around the arguments, but if you omit it you must not include the parentheses.

Quote from 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.

To pass a whole array to a procedure, use the array name followed by empty parentheses.

Link: https://msdn.microsoft.com/en-us/library/office/gg251710.aspx


In Practice

This means that the following syntaxes are allowed:

Call abc("aaa")
Call abc("aaa", 2)
abc "aaa", 2
abc("aaa") ' <- Parantheses here do not create an argument list
abc(((("aaa")))) ' <- Parantheses here do not create an argument list

The following syntaxes are not allowed:

Call abc "aaa", 2
abc("aaa", 2) ' <- Parantheses here create an argument list

Function Return Values

This doesn't take effect when using a function to get a return value, for example if you were to do the following you need parentheses:

Function abc(a As String, Optional iCol As Long = 3)
    abc = iCol
End Function

'## IMMEDIATE WINDOW ##
?abc("aaa", 2)      'this works
?abc "aaa, 2        'this will not work
?Call abc "aaa", 2  'this will not work
?Call abc("aaa", 2) 'this will not work

If you are using Call on a Function then consider changing it to a Sub instead, functions are meant to return a value as in the cases above.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this would work:

Option Explicit

Public Sub Test()

    Dim strText As String

    strText = "E"
    Call Abc(strText, 3)
    Call Abc(strText, 2)

    Abc (strText)
'    Abc (strtext,5)

    Abc strText, 2
    Abc strText


End Sub

Public Sub Abc(strText As String, Optional iCol As Long = 5)

    Debug.Print iCol

End Sub

Absolutely no idea why the commented code does not work...

Comments

0

Call is an optional keyword, as already described in detailed in the answer above.

for your second option

abc("aaa", 2) ' is a syntax error

Just use:

abc "aaa", 2 

Note: there is little use to have a Function if you don't return anything, you could have a regular Sub instead.

to have a Function that return a String for instance (just something made-up quick):

Function abc(a As String, Optional iCol As Long = 3) As String                    
    abc = a & CStr(iCol)        
End Function

and then call it:

Public Sub Test()

    d = abc("aaa", 2)
    MsgBox d

End Sub

1 Comment

Because the paretheses don't do anything when there's only one value, it's not creating an argument list it's encapsulating what is inside to one value. To demonstrate, this will also work: abc ((((("aaa")))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.