Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Passing a Function name as a procedure argument

1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Passing a Function name as a procedure argument

Not if I understand you correctly, but you could pass an id and check that
with select Case and run the function accordingly

Select Case thisId
Case 1: myVal = Func1(a,b)
Case 2: myVal = Func2(a,b)
'etc.
End Select

--

HTH

RP
(remove nothere from the email address if mailing direct)


" wrote in message
oups.com...
1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Passing a Function name as a procedure argument

Thanks Bob, for a good robust solution. But would it be possible to do
it more elegantly like:

myValy= Fun1(a,b)

where "Fun1(a,b)" is passed in as a string or an object in the
parameter list?

Then if I write an new procedure, Fun3(c,d), I will not have to add it
to a case statement in the GetARoot procedure.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Passing a Function name as a procedure argument

No, that is exactly the point I was making, you cannot do that.

Here is another alternative, but only becomes useful I guess if all
functions have the same number of arguments

Sub TestFuncEvaluator()
Dim sFunction As String

sFunction = "func1"
Debug.Print Application.Run(sFunction, 11, 22)

sFunction = "func2"
Debug.Print Application.Run(sFunction, 11, 22)


End Sub

Function func1(val1, val2)
func1 = val1 * val2
End Function

Function func2(val1, val2)
func2 = val1 + val2
End Function

--

HTH

RP
(remove nothere from the email address if mailing direct)


" wrote in message
oups.com...
Thanks Bob, for a good robust solution. But would it be possible to do
it more elegantly like:

myValy= Fun1(a,b)

where "Fun1(a,b)" is passed in as a string or an object in the
parameter list?

Then if I write an new procedure, Fun3(c,d), I will not have to add it
to a case statement in the GetARoot procedure.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default Passing a Function name as a procedure argument

This is probably bad programming practice, but are there any ideas here that
can help?
Just step thru "TestIt" to follow the basic ideas. Good luck.

Sub TestIt()
'// Just call BasicFx
Debug.Print BasicFx("MyFx1", 2, 3)
Debug.Print BasicFx("MyFx2", 2, 3)
End Sub

Function BasicFx(Fx As String, x, y)
BasicFx = Application.Run(Fx, x, y)
End Function

Function MyFx1(a, b)
MyFx1 = a + b
End Function

Function MyFx2(a, b)
MyFx2 = a ^ 2 + b
End Function

HTH. :)
--
Dana DeLouis
Win XP & Office 2003


" wrote in message
oups.com...
1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Passing a Function name as a procedure argument

WJG,

A good way to do this is to place your function(s) in a class module. Then
you can use the CallByName VBA method to "call" the desired function using a
string value...

Module Module1:
Sub test()
Dim M As New MathLibrary

CallByName M, "FuncA", VbMethod, 2345, 4321
CallByName M, "FuncB", VbMethod, 2345
End Sub

Class MathLibrary:
Public Function FuncA(a As Integer, b As Integer)
MsgBox "FuncA(" & a & "," & b & ")"
End Function
Public Function FuncB(a As Integer)
MsgBox "FuncB(" & a & ")"
End Function

BJ

" wrote in message
oups.com...
1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Passing a Function name as a procedure argument

Sorry for the double post but the following example is bit more to what you
need:

Module Module1:
Sub test()
Dim M As New MathLibrary

result1 = CallByName(M, "FuncA", VbMethod, 2345, 4321)
result2 = CallByName(M, "FuncB", VbMethod, 2)
End Sub


Class MathLibrary:
Public Function FuncA(a As Double, b As Double) As Double
FuncA = a / b
End Function
Public Function FuncB(a As Double) As Double
FuncB = a ^ 2
End Function


" wrote in message
oups.com...
1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Passing a Function name as a procedure argument

And I think you need xl2002 or higher to use CallByName.

"Mark H. Shin" wrote:

Sorry for the double post but the following example is bit more to what you
need:

Module Module1:
Sub test()
Dim M As New MathLibrary

result1 = CallByName(M, "FuncA", VbMethod, 2345, 4321)
result2 = CallByName(M, "FuncB", VbMethod, 2)
End Sub

Class MathLibrary:
Public Function FuncA(a As Double, b As Double) As Double
FuncA = a / b
End Function
Public Function FuncB(a As Double) As Double
FuncB = a ^ 2
End Function

" wrote in message
oups.com...
1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?


--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Passing a Function name as a procedure argument

Nah, 2000 has it.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Dave Peterson" wrote in message
...
And I think you need xl2002 or higher to use CallByName.

"Mark H. Shin" wrote:

Sorry for the double post but the following example is bit more to what

you
need:

Module Module1:
Sub test()
Dim M As New MathLibrary

result1 = CallByName(M, "FuncA", VbMethod, 2345, 4321)
result2 = CallByName(M, "FuncB", VbMethod, 2)
End Sub

Class MathLibrary:
Public Function FuncA(a As Double, b As Double) As Double
FuncA = a / b
End Function
Public Function FuncB(a As Double) As Double
FuncB = a ^ 2
End Function

" wrote in message
oups.com...
1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?


--

Dave Peterson



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Passing a Function name as a procedure argument

Thanks for the correction.



Bob Phillips wrote:

Nah, 2000 has it.

--

HTH

RP
(remove nothere from the email address if mailing direct)

"Dave Peterson" wrote in message
...
And I think you need xl2002 or higher to use CallByName.

"Mark H. Shin" wrote:

Sorry for the double post but the following example is bit more to what

you
need:

Module Module1:
Sub test()
Dim M As New MathLibrary

result1 = CallByName(M, "FuncA", VbMethod, 2345, 4321)
result2 = CallByName(M, "FuncB", VbMethod, 2)
End Sub

Class MathLibrary:
Public Function FuncA(a As Double, b As Double) As Double
FuncA = a / b
End Function
Public Function FuncB(a As Double) As Double
FuncB = a ^ 2
End Function

" wrote in message
oups.com...
1. I have a Standard Module Function:
Public Function GetARoot(ByVal FirstGuess As Double, ByVal
FunctionName As _
String) As Double
that calculates the root of an equation.

I have several equations programmed as Functions in the Standard
Module. I wish to pass the name of one of these Functions as an
argument to "FunctionName" and call the Function with this name from
inside GetARoot.

Can this be done?


--

Dave Peterson


--

Dave Peterson


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 range as an argument in a function Hari[_3_] Excel Programming 1 June 15th 04 02:41 AM
VBA - Passing a FUNCTION as an Argument James B Excel Programming 3 February 18th 04 03:42 PM
Passing Userform as an argument to a procedure Howard Kaikow Excel Programming 12 October 24th 03 03:24 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 10:27 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"