Help:Using strings to call subroutines

K

KAB

Hi All
I'm wondering if any body knows how to call a subroutine from a string. For
example:

Dim TestString as string
TestString = "MySubroutine(Data)"
'***The question I have would be: How to use TestString to call
MySubroutine(Data).

Thank You
KAB
 
G

Greg Maxey

Something like this perhaps:

Sub ScratchMacro()
Dim TestString As String
Dim PassString As String
PassString = "Who's your uncle?"
TestString = MySubRoutine(PassString)
MsgBox TestString
End Sub

Function MySubRoutine(Data As String)
MySubRoutine = "Bob's your uncle"
End Function
 
C

crz

Is this what you meant?

Sub Test()
Dim s As String

s = "Curly"
Application.Run s

s = "Larry"
Application.Run s

s = "Moe"
Application.Run s
End Sub

Sub Curly()
MsgBox "I'm Curly"
End Sub

Sub Larry()
MsgBox "I'm Larry"
End Sub

Sub Moe()
MsgBox "I'm Moe"
End Sub


You can also pass parmeters to those macros as well. See the docs for details.
 
K

Klaus Linke

KAB said:
Hi All
I'm wondering if any body knows how to call a subroutine from a string.
For
example:

Dim TestString as string
TestString = "MySubroutine(Data)"
'***The question I have would be: How to use TestString to call
MySubroutine(Data).

Thank You
KAB


Hi KAB,

Also look at the VBA help on CallByName ... You'd need to pass the macro
name and arguments separately though.

Klaus
 
Top