VBA Call method with optional parameters -


i figured out setting optional parameters requires "call" infront of method.

public sub test()      call abc("aaa")     call abc("aaa", 2)      abc("aaa") ' fine     abc("aaa", 2) ' syntax error  end sub   function abc(a string, optional icol long = 3)      msgbox (icol)  end function 

can add "why make sense?" new information?

greetings, peter

edit: ps function abc no other use simplify question.

documentation

call optional keyword, 1 caveat if use must include parentheses around arguments, if omit must not include parentheses.

quote msdn:

you not required use call keyword when calling procedure.

however, if use call keyword call procedure requires arguments, argumentlist must enclosed in parentheses. if omit call keyword, must omit parentheses around argumentlist. if use either call syntax call intrinsic or user-defined function, function's return value discarded.

to pass whole array procedure, use array name followed empty parentheses.

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


in practice

this means following syntaxes allowed:

call abc("aaa") call abc("aaa", 2) abc "aaa", 2 abc("aaa") ' <- parantheses here not create argument list abc(((("aaa")))) ' <- parantheses here not create argument list 

the following syntaxes not allowed:

call abc "aaa", 2 abc("aaa", 2) ' <- parantheses here create argument list 

function return values

this doesn't take effect when using function return value, example if following need parentheses:

function abc(a string, optional icol long = 3)     abc = icol end function  '## immediate window ## ?abc("aaa", 2)      'this works ?abc "aaa, 2        'this not work ?call abc "aaa", 2  'this not work ?call abc("aaa", 2) 'this not work 

if using call on function consider changing sub instead, functions meant return value in cases above.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -