Re: Function shortcut help



Helmut, and Tony:

This is exactly what I was looking for. Sorry for the confusing post, at
the time I was trying to explain the syntax to a fellow Java developer (we
couldn't find an example on google) so my question was very rushed. Thanks
for the quick repleis!



"Jay Freedman" <jay.freedman@xxxxxxxxxxx> wrote in message
news:%23uUKiSVUIHA.5836@xxxxxxxxxxxxxxxxxxxxxxx
Hi Helmut,

The poster (ext237) is so confused that he's confused you, too. ;-) I
think the question was not about optional arguments, either. It seems to
be asking about passing arguments by position versus passing them by name.

There's a topic in the VBA help about this, called "Understanding Named
and Optional Arguments". It begins with this:

"When you call a Sub or Function procedure, you can supply arguments
positionally, in the order they appear in the procedure's definition, or
you can supply the arguments by name without regard to position."

It then gives a number of examples.

One thing the original post had wrong was using a plain equal sign in the
call using named arguments. VBA requires the := operator in named argument
assignments.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.

Helmut Weber wrote:
Hi,

IMHO objects don't have parameters.
They have properties and methods.
But that's not what you are asking about,
neither is is about a shortcut.

It seems to be that:

Public Function MyResult(x As String, Optional y As String)
MyResult = x & y
End Function

Sub Test9003()
MsgBox MyResult("Yes")
End Sub

See help on function statement:

"Optional" is a part of the argument list of a function

"Optional" itself is optional:

Indicates that an argument is not required. If used, all subsequent
arguments in arglist must also be optional and declared using the
Optional keyword. Optional can't be used for any argument if
ParamArray is used.

Try also:
Public Function MyResult(x As String, Optional y As String, _
Optional z As String)
MyResult = x & y
End Function

and:

Public Function MyResult(Optional x As String, y As String)
' no way!
MyResult = x & y
End Function

If an argument it optional,
following arguments must be optional as well.


HTH




.



Relevant Pages


Loading