Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Array as Argument in User-Defined Functions

An alternative to Harlan's suggestion would be to put the x value
first, and then use a ParamArray. For more on ParamArray see the Sub
statement in XL VBA help. One compiled but otherwise untested sample:

Option Explicit

Function Fx(ByVal x As Double, ParamArray Coeff() As Variant)
Dim i As Integer, Power As Integer
Power = 0
For i = LBound(Coeff) To UBound(Coeff)
Fx = Fx + Coeff(i) * x ^ Power
Power = Power + 1
Next i
End Function

And, for all those tempted to point out that I could have just as
easily used i instead of Power, yes, it may be possible. However, I am
not sure if the lower bound of a paramarray is guaranteed to be zero.
Using my own variable works around the uncertainty.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
I am trying to write a user-defined function that calculates a complex function, based on a given set of coefficients. For simplicity, let's say the equation is y=a + bx + cx^2. I would like to set up the user-defined function so that the argument for the coefficients can be input as a range. Thinking something like this:

Function Polynomial(Coefficients,X_value)
Polynomial=Coefficient(1,0) + Coefficient(2,0)*X_value + Coefficient(3,0)*X_value^2
End Function

Which would be a lot handier for my application than having to set up the code as

Function Polynomial(a,b,c,X_Value)
Polynomial=a + b*X_value + c*X_value^2
End Function

The actual function is more complex, and contains some logic testing requirements. So I am trying to avoid writing a function that requires 15 input arguments. Can you offer some insight into a classy method to accomplish this?

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
User defined functions without using VBA. [email protected] Excel Worksheet Functions 0 June 13th 06 05:49 PM
User Defined Functions Mike McLellan Excel Worksheet Functions 2 May 4th 06 10:56 AM
User Defined Functions Frank@shell Excel Worksheet Functions 3 April 20th 05 02:41 PM
Array as Argument in User-Defined Functions Harlan Grove[_5_] Excel Programming 0 May 18th 04 10:30 PM
Passing an Array of User-Defined Type to an Argument of a Function Tushar Mehta[_6_] Excel Programming 0 August 17th 03 06:43 PM


All times are GMT +1. The time now is 04:37 AM.

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

About Us

"It's about Microsoft Excel"