ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Optional Arguements for Functions (https://www.excelbanter.com/excel-programming/290176-optional-arguements-functions.html)

ExcelMonkey[_70_]

Optional Arguements for Functions
 
I want to build a function which takes two optional arguements. If Y
and Z are note provided, I would like to make them equal to 1. How do
I state this within the function? They way I have it below will force
them to 1 regardless.

Private Function New(X As Double, Option Y As Double, Optional Z As
Double)

'if Y and Z are not provided then force them to value of 1
Y = 1.0
Z = 1.0

New = X*Y*Z


---
Message posted from http://www.ExcelForum.com/


Tom Ogilvy

Optional Arguements for Functions
 
Private Function New(X As Double, Option Y As Double = 1, _
Optional Z As Double = 1)

New = X*Y*Z

End Function

--
Regards,
Tom Ogilvy



ExcelMonkey wrote in message
...
I want to build a function which takes two optional arguements. If Y
and Z are note provided, I would like to make them equal to 1. How do
I state this within the function? They way I have it below will force
them to 1 regardless.

Private Function New(X As Double, Option Y As Double, Optional Z As
Double)

'if Y and Z are not provided then force them to value of 1
Y = 1.0
Z = 1.0

New = X*Y*Z


---
Message posted from http://www.ExcelForum.com/




Ron Rosenfeld

Optional Arguements for Functions
 
On Tue, 3 Feb 2004 21:17:19 -0600, ExcelMonkey
wrote:

I want to build a function which takes two optional arguements. If Y
and Z are note provided, I would like to make them equal to 1. How do
I state this within the function? They way I have it below will force
them to 1 regardless.

Private Function New(X As Double, Option Y As Double, Optional Z As
Double)

'if Y and Z are not provided then force them to value of 1
Y = 1.0
Z = 1.0

New = X*Y*Z



Try:

Function Nw(X As Double, Optional Y As Double = 1, Optional Z As Double = 1)

Nw = X * Y * Z

End Function

--ron

Rob van Gelder[_4_]

Optional Arguements for Functions
 
Just to add.
Optional arguments are only valid at the end. In other words, you can't have
mandatory arguments following optional ones.


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Tom Ogilvy" wrote in message
...
Private Function New(X As Double, Option Y As Double = 1, _
Optional Z As Double = 1)

New = X*Y*Z

End Function

--
Regards,
Tom Ogilvy



ExcelMonkey wrote in message
...
I want to build a function which takes two optional arguements. If Y
and Z are note provided, I would like to make them equal to 1. How do
I state this within the function? They way I have it below will force
them to 1 regardless.

Private Function New(X As Double, Option Y As Double, Optional Z As
Double)

'if Y and Z are not provided then force them to value of 1
Y = 1.0
Z = 1.0

New = X*Y*Z


---
Message posted from http://www.ExcelForum.com/







All times are GMT +1. The time now is 06:27 PM.

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