Application.run can be used to dynamically run a sub or function by a name stored in a variable.
The optional parameters of run() are passed to the sub/function being called.
In case of a function, the return value of the function is returned also from run()
option explicit
sub main()
application.run "runSub", "subTwo"
debug.print application.run("funcOne", 19 , 23 )
debug.print application.run("funcTwo", "Hello", "World")
end sub
sub runSub(subName as string)
debug.print "runSub tries to run " & subName
application.run subName
end sub
sub subOne()
debug.print "subOne was called"
end sub
sub subTwo()
debug.print "subTwo was called"
end sub
function funcOne(a as long, b as long) as long
funcOne = a + b
end function
function funcTwo(a as string, b as string) as string
funcTwo = a & " " & b
end function
Running a sub in another workbook
It's possible to run a macro in another (possibly unopened) workbook: