Does VBA support keyword parameters?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
Does VBA support any kind of "keyword" parameter? By this, I mean parameters of the form keyword=value. Something like this:

Code:
=MyFun(A1, B2, Keyword4=C3, Keyword2=D4, ...)

I am working on a UDF that has half a dozen optional parameters with more to come. Most of them are omitted on most calls, taking the default value. As currently coded, this means that I have to remember the order, which is error prone and tedious. It also means that if I add a parameter, it has to go at the end, even if it will be the most frequently used parameter, or else I have to find and edit all previous calls.

If I could code them with keywords, I could enter them in any order and they would be self-documenting. I could also add parameters without having to change any existing calls.

I know I can do this with a Paramarray parameter, but then I have to parse the string. I was hoping that there might be an easier way.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You can pass arguments to a UDF by named Parameters.
Here is a simple example

Code:
Function MyFunction(First As String, Optional Second As String = "2nd", Optional Third As String = "3rd")
    MyFunction = First & "," & Second & ", " & Third
End Function

Sub test()
    MsgBox MyFunction("cat", "dog", "fish")
    MsgBox MyFunction("cat", , "fish")
    MsgBox MyFunction(First:="cat", Third:="fish")
End Sub
 
Upvote 0
Thanks, guys.

Names parameters looks like exactly what I need, but for some reason I can't get it to work.

Here's my test function:

Code:
Function NamedParmTest(P1, P2, Optional P3 As String = "?", _
                                  Optional P4 = "?", Optional P5 As String = "?")
NamedParmTest = "P1=" & P1 & ", P2=" & P2 & ", P3=" & P3 & ", P4=" & P4 & ", P5=" & P5
End Function

It works fine if I pass the parameters by position, but fails if I try passing a parameter by name.

R/C
CD
3Function CallFormula
4P1=A, P2=b, P3=?, P4=?, P5=?C4: =NamedParmTest("A", "b")
5P1=A, P2=b, P3=C, P4=?, P5=?C5: =NamedParmTest("A", "b", "C")
6P1=A, P2=b, P3=C, P4=d, P5=EC6: =NamedParmTest("A", "b", "C", "d", "E")
7Error
C7: =NamedParmTest("A", "b", P4:="d")

<tbody>
</tbody>

The function call in C7 as depicted by the expression in D7, gets an error.

What am I doing wrong?
 
Upvote 0
Named parameters only works within the VB world... because of syntax differences, it will not work inside of the Excel world.
 
Upvote 0
****! It would have been such a great solution. But thanks for helping me worry about whether I was going insane.
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,161
Members
448,948
Latest member
spamiki

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top