Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 486
Default 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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35
Default 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!
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
optional calculations roger_the_dodger Excel Worksheet Functions 1 October 6th 08 10:51 PM
Optional hyphen? Eric Excel Discussion (Misc queries) 4 October 19th 06 01:31 AM
Optional Linking Vin81 Excel Discussion (Misc queries) 0 February 21st 06 12:56 AM
Application.Run("MacroName", Args) Abraham Andres Luna Excel Programming 2 January 23rd 06 07:05 PM
Over 29 args in a UDF with double parens Greg Lovern[_3_] Excel Programming 1 April 25th 05 09:46 PM


All times are GMT +1. The time now is 01:42 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"