ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   optional args in UDF (https://www.excelbanter.com/excel-programming/354891-optional-args-udf.html)

masterphilch

optional args in UDF
 
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

Jim Thomlinson[_5_]

optional args in UDF
 
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

--
HTH...

Jim Thomlinson


"masterphilch" wrote:

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


Bob Phillips[_6_]

optional args in UDF
 
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)

"masterphilch" wrote in message
...
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




Chip Pearson

optional args in UDF
 
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



"masterphilch" wrote in message
...
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




masterphilch

optional args in UDF
 
Chip Pearson schrieb:
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!


All times are GMT +1. The time now is 12:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com