7

The below example the optional arguments must be given in order; hence ShowHeaders Must Precede ValueAdd and so on. If I want to specify ValueAdd, I*must* specify ShowHeaders:

Function Example(Value1, Optional ShowHeaders = "Headers=N", Optional ValueAdd = "Sprd=0")

I want to be able to specify one or more of a (relatively) large list of optional arguments: 1) but not in order, and 2) not necessarily all of them.

For 1) I was thinking, perhaps make a list of arguments generic, eg rather than the above do:

Function Example(Value1, Optional Arg1, Optional Arg2)

Then subsequently check if the leftmost section of Arg1 = "Headers=" or "Sprd=" and so on, and then do the same for Arg2. This is fine but doesn't seem terribly efficient and I'd be planning on creating UDFs with > 10 optional arguments. The above solution would also address 2) but I just don't feel its very good coding.

For 2) I know we can use

If IsMissing(Arg) Then

but this doesn't really address the order we specify functions.

1
  • Tried Example(Value1:="", ValueAdd:="Sprd=0")? Or Example(Value1,Arg2:="A2")? Commented Aug 26, 2013 at 23:09

2 Answers 2

6

You can use the := operator along with the name of the variable. This way you only need to send the optional values that are specific to that call. Using your above example you could use:

Call Example(Value1, ValueAdd := "Sprd=0")

And this way you don't have to enter anything about showheaders etc.

Edit:

I've modified your example to handle the missing arguments so that they can be used for maths, hope this helps.

Function Example(Value1, Optional ValueA, Optional ValueB)

    If IsMissing(ValueB) Then ValueB = 0
    If IsMissing(ValueA) Then ValueA = 0

    Example = (Value1 + ValueA) * ValueB)
    MsgBox (Example)

End Function

Sub TestExample()
    Call Example(2, ValueB:=1)
    Call Example(2, ValueB:=1, ValueA:=6)
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, not sure I understand how this works within Function (largely due to my relative lack of knowledge of VBA. For instance, how could I employ this with the code: Function Example(Value1, Optional ValueA = "0", Optional ValueB = "1") Example = (Value1 + Value2) * Value3 End Function if I wanted to specify in Excel =Example(2, "ValueB=5") to return 10 (ie (2 + 0) * 5) thus omitting the optional argument ValueA, and recognizing that the argument I specified was for ValueB? Thanks kindly for your response.
0

I have tried this in a recent macro, It is the manner in which such functions are called within VBA code. Your arguments that are optional can be skipped by the use of strategically placed commas (,) in the call.

Case "do not pass ValueA" ...  =Example(2,,5)
Case "do not pass ValueB" ...  =Example(2,7)   

Since only two args are present, the function will recognize that ValueB is optional and will 'see' only Value1 and ValueA.

Case "do not pass ValueA or ValueB" ...  =Example(2)  

See comment for "do not pass ValueB ... =Example(2,7)"

Comments

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.