Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default VBA - Passing a FUNCTION as an Argument

Some expert help is required on this one. I want to
create a VBA function that calculates the derivative of
another function between two points.

The syntax would be something like the following:

Gradient = DerFun(Height(), x1, x2)

where

Function DerFun(Arg_Function as function, point1, point2)
DerFun=(Arg_Function(point2)-ArgFunction(point1))/ _
(point2-point1)
End Function

What is the proper syntax, instead of "Arg_Function as
Function"?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,440
Default VBA - Passing a FUNCTION as an Argument

Look at the evaluate() function

--

Kind Regards,

Niek Otten

Microsoft MVP - Excel

"James B" wrote in message
...
Some expert help is required on this one. I want to
create a VBA function that calculates the derivative of
another function between two points.

The syntax would be something like the following:

Gradient = DerFun(Height(), x1, x2)

where

Function DerFun(Arg_Function as function, point1, point2)
DerFun=(Arg_Function(point2)-ArgFunction(point1))/ _
(point2-point1)
End Function

What is the proper syntax, instead of "Arg_Function as
Function"?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default VBA - Passing a FUNCTION as an Argument

James,

You can't pass function pointers in VBA. The closest you can get
is to pass the name of the procedure as an string and use
Application.Run to run the procedure. E.g.,

Function DerFunction(F As String, Point1,Point2)
DerFunction = Application.Run(F)
End Function

DerFunction "TheFunctionName", 1, 2


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


"James B" wrote in message
...
Some expert help is required on this one. I want to
create a VBA function that calculates the derivative of
another function between two points.

The syntax would be something like the following:

Gradient = DerFun(Height(), x1, x2)

where

Function DerFun(Arg_Function as function, point1, point2)
DerFun=(Arg_Function(point2)-ArgFunction(point1))/ _
(point2-point1)
End Function

What is the proper syntax, instead of "Arg_Function as
Function"?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA - Passing a FUNCTION as an Argument

James, A function is not a datatype, so you cannot say Arg_Function as
Function, you have to use the datatype that the function returns, or
variant.

Here is a simple working example

Sub test()
MsgBox Func1(Func2, 9)
End Sub

Function Func2() As Long
Func2 = 7
End Function

Function Func1(arg1, arg2 As Long) As Long
Func1 = arg1 * arg2
End Function



--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"James B" wrote in message
...
Some expert help is required on this one. I want to
create a VBA function that calculates the derivative of
another function between two points.

The syntax would be something like the following:

Gradient = DerFun(Height(), x1, x2)

where

Function DerFun(Arg_Function as function, point1, point2)
DerFun=(Arg_Function(point2)-ArgFunction(point1))/ _
(point2-point1)
End Function

What is the proper syntax, instead of "Arg_Function as
Function"?



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
Passing a range name as an argument to the Index Function Michael Sharpe Excel Discussion (Misc queries) 3 September 5th 12 01:33 PM
Passing a UDF as an argument to a UDF puff Excel Discussion (Misc queries) 3 February 23rd 06 09:46 PM
Passing range as argument Jan Kronsell[_2_] Excel Programming 3 September 3rd 03 12:31 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
passing a variable as an argument to a function Drew[_6_] Excel Programming 3 July 25th 03 08:51 PM


All times are GMT +1. The time now is 09:34 PM.

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"