optional args in UDF

M

masterphilch

Hi

How can I add optional arguments to a UDF?

Function myfunc(arg1,arg2,[arg3])
->that with theses squared brackets doesn't work...

thanks for help
masterphilch
 
G

Guest

Give this a try. I have assigned a default value to the optional argument (if
one is not supplied)...

Public Function MyFunction(ByVal Arg1 As String, _
ByVal Arg2 As String, _
Optional ByVal Arg3 As String = "Tada")

MyFunction = Arg1 & Arg2 & Arg3
End Function
 
B

Bob Phillips

You're looking at thge definition syntax, not how code is written.

Function myfunc(arg1, arg2, Optional arg3)

If IsMissing arg3 Then
'do something
End If

.....

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
C

Chip Pearson

Use the word Optional.

Function MyFunction(X As Long, Y As Long, Optional Z As Long)

Optional arguments must be the last arguments in the list. You
can also provide a default value for the argument, to be used if
the argument is omitted. E.g,

Function MyFunction(X As Long, Y As Long, Optional Z As Long =
123)

If you declare the optional argument As Variant, you can use
IsMissing to determine whether the argument was supplied. E.g.,

Function MyFunction(X As Long, Y As Long, Optional Z As Variant)
If IsMissing(Z) = True Then
Debug.Print "Z is missing"
End If
End Function


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
M

masterphilch

Chip said:
Use the word Optional.

Function MyFunction(X As Long, Y As Long, Optional Z As Long)

Optional arguments must be the last arguments in the list. You
can also provide a default value for the argument, to be used if
the argument is omitted. E.g,

Function MyFunction(X As Long, Y As Long, Optional Z As Long =
123)

If you declare the optional argument As Variant, you can use
IsMissing to determine whether the argument was supplied. E.g.,

Function MyFunction(X As Long, Y As Long, Optional Z As Variant)
If IsMissing(Z) = True Then
Debug.Print "Z is missing"
End If
End Function
thanks for all replies!
I just didn't think, that vba is a language including words like 'Optional'.

thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top