Run a Subroutine From a string Variable

  • Thread starter Thread starter Ross
  • Start date Start date
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!
 
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.
 
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!!!!!
 
not sure abour Sub, but function you can call using application.run or
eval()
check online help
 
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.
 
Back
Top