ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   optional argument in a function (https://www.excelbanter.com/excel-programming/329266-optional-argument-function.html)

visitor

optional argument in a function
 
hi,

can anyone advise as to how do i use an optional argument in a function?

thanks in advance

Niek Otten

optional argument in a function
 
What do you want to do? And what are you uncertain about?

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel

"visitor" wrote in message
...
hi,

can anyone advise as to how do i use an optional argument in a function?

thanks in advance




Chip Pearson

optional argument in a function
 
Try something like the following:


Function Test(Optional X As Variant) As Integer
If IsMissing(X) Then
Test = 0
Else
Test = X + 1
End If
End Function

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



"visitor" wrote in message
...
hi,

can anyone advise as to how do i use an optional argument in a
function?

thanks in advance




Niek Otten

optional argument in a function
 
Note that this only works correctly if you declare the optional argument as
Variant, or omit any declaration, which results in the argument being
treated as a Variant too.

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel

"Chip Pearson" wrote in message
...
Try something like the following:


Function Test(Optional X As Variant) As Integer
If IsMissing(X) Then
Test = 0
Else
Test = X + 1
End If
End Function

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



"visitor" wrote in message
...
hi,

can anyone advise as to how do i use an optional argument in a function?

thanks in advance






Chip Pearson

optional argument in a function
 
Just to follow up, the argument must be Variant only if you want
to use IsMissing. Optional arguments themselves may be of any
data type. If an argument is omitted, its default value (0, "",
or Nothing) will be used.

Optional argument must follow required arguments.

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


"Niek Otten" wrote in message
...
Note that this only works correctly if you declare the optional
argument as Variant, or omit any declaration, which results in
the argument being treated as a Variant too.

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel

"Chip Pearson" wrote in message
...
Try something like the following:


Function Test(Optional X As Variant) As Integer
If IsMissing(X) Then
Test = 0
Else
Test = X + 1
End If
End Function

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



"visitor" wrote in message
...
hi,

can anyone advise as to how do i use an optional argument in
a function?

thanks in advance









All times are GMT +1. The time now is 10:35 AM.

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