Run a Subroutine From a string Variable

R

Ross

I have the name of a subroutine established as a string
variable. How can I use the string variable to call /
run that subroutine?

Can you define (Dim) a variable as a subroutine or
function to do this?

Thank you!
 
M

Microsoft News Groups

Ross,

You can use the Eval() function.

eg.

Public Function ExecFn(ByVal strFn As String, Optional ByVal strArgs As
String) As Variant
Dim strToExecute As String

strToExecute = strFn & "(" & strArgs & ")"
ExecFn = Eval(strToExecute)
End Function

Public Function AddOne(ByVal lngVal As Long) As Long
AddOne = lngVal + 1
End Function

Public Function Test()
debug.print ExecFn("AddOne","2")
End Function

When run returns 3.

The strFn parameter should contain the name of the function and strArgs
should contain the parameters to passed to the function separated by commas.
Any string parameters should be enclosed in single quotes or a pair of
double quotes.

Rod Scoullar.
 
G

Guest

Rod,

If you knew how much pressure I was under and how little
time I have to research this, you would understand fully
the depths of my appreciation.

Thank you!!!!!
 
A

Alex Dybenko

not sure abour Sub, but function you can call using application.run or
eval()
check online help
 
M

Marshall Barton

Ross said:
I have the name of a subroutine established as a string
variable. How can I use the string variable to call /
run that subroutine?

Can you define (Dim) a variable as a subroutine or
function to do this?


For Sub procedures, you can use the Run method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top