VBA Syntax

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

Hi Folks - I have seen 2 syntax uses in modules when using certain builtin
functions such as MsgBox. Here's an example:

MsgBox (prompt: "What is your age?")
MsgBox ("What is your age?")

The first example uses positional keywords with a colon. The second example
does not. Are there differences? Is the first example from an older version
of VBA? Thanks.

Michael
 
The first example is not correct.
It is using a Named argument, but it needs an = after the :. Note, they
don't have to be in order. For example:

msgbox buttons:= vbquestion, Title:="Whoo", prompt:= "Bing"

Will work. The names used are the name of the arguments you will see in
Help files or in Intellisense. You can do the same thing with subs or
functions you write. It allows you to identify only the arguments you want
to use without having to include commas for missing arguments.
 
Sorry about the typos .... Which method is "best practice"? I've been
dallying in VBA for a few years, but have never used the first method. It
seems I only see the first method in tutorials and manuals.

Mike
 
I seldom use named arguments and don't see them used much. The only time I
use them is with a function that calles the Open Common Dialog Box API
because there are a ton of arguments and I find it easier to use named
arguments in that case rather than remember the order or count all the commas.
 
Back
Top