Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

excel vba: Special Types - Functions as Arguments of Functions

Tags:

There is no special type for functions in VBA. It is hard for me to see how to add functions as arguments to functions in Excel VBA.

What I am trying to accomplish is something like this:

function f(g as function, x as string) as string         f = g(x) end function 

Currently, I have a group of little functions all repeating themselves but with one call to a specific function.

like image 598
Roman Glass Avatar asked Jul 13 '09 08:07

Roman Glass


People also ask

Can you pass function as parameter in VBA?

You can't actually pass functions in VBA unfortunately and OpenPulledReports() thinks it is getting passed 2 values.

What are the two methods to pass arguments in VBA?

VBA allows you to pass variables into subroutines and functions in two ways. You can specify either ByVal or ByRef for each of the variables that are passed in. The ByVal and ByRef distinction for subroutine and function parameters is very important to make. In VBA all objects are passed by reference.

How do you call a function from another module in VBA?

To call a macro or function that is in the same workbook (it need not be in the same module) just type the name of the macro/function and any arguments it requires on one line of the calling macro. Another way is to prefix the macro/function called with the word Call.

How do you define a sub or a function in VBA?

When writing the sub, we use the “Sub” keyword and a name as the procedure name to declare the sub. The sub procedure should be followed by the task to be performed, written in VBA language. The sub should close with the statement End Sub. End Sub.


2 Answers

From your code, function g takes a string parameter and returns a string. I suggest you create a class module called IStringFunction to act as the definition of an interface that all functions will support, thus:

Class Module: IStringFunction

Public Function Evaluate(ByVal s As String) As String End Function 

Then, create a couple of example functions implementing this interface:

Class Module: HelloStringFunction

Implements IStringFunction  Public Function IStringFunction_Evaluate(ByVal s As String) As String     IStringFunction_Evaluate = "hello " & s End Function 

Class Module: GoodbyeStringFunction

Implements IStringFunction  Public Function IStringFunction_Evaluate(ByVal s As String) As String     IStringFunction_Evaluate = "goodbye " & s End Function 

...and finally, some test code to exercise the functions:

(Standard) Module: Test

Sub Test()      Dim oHello As New HelloStringFunction     Dim oGoodbye As New GoodbyeStringFunction      MsgBox Evaluate(oHello, "gary")     MsgBox Evaluate(oGoodbye, "gary")  End Sub  Private Function Evaluate(ByVal f As IStringFunction, ByVal arg As String) As String     Evaluate = f.Evaluate(arg) End Function 

Note that the class implementing the interface must have methods named <Interface>_<Method> as in the example above, not just <Method> as you'd expect.

Download the simple demo or intermediate demo here

like image 156
Gary McGill Avatar answered Oct 21 '22 13:10

Gary McGill


Since VBA has it's roots in an interactive language, it has always had the ability to execute text:

function f(g as string, x as string) as string         f = application.run(g,x) end function  MyStringA = f("functionA",string1) MyStringB = f("functionB",string1) 

Edit to Add: I think that in current versions of Excel, the application (Excel) can 'run' only things you can show in a spreadsheet cell. So that means functions, not subroutines. To execute a subroutine, wrap it up in a function wrapper:

function functionA(x as string)     Call MySubA(x) end function 
like image 21
david Avatar answered Oct 21 '22 14:10

david