Article ID: 108519
Article Last Modified on 10/10/2006
Application.Run ("TestXLM")
If you have the variable "MacroToRun" set to "TestXLM," you could use this
method:
Application.Run (MacroToRun)
Application.ExecuteExcel4Macro "RUN(""TestXLM"")"
-or-
Application.ExecuteExcel4Macro "RUN(""" & MacroToRun & """)"
Note that when you use Application.ExecuteExcel4Macro, you must use
quotation marks. For example, to use the RUN() function, you must enclose
the name of the argument in quotation marks:
RUN("TestXLM")
Because the entire string must also be enclosed in quotation marks, when
you add quotation marks to the outside of the string, you must also add an
additional quotation mark adjacent to each quotation mark within the
string. The resulting string is as follows:
"RUN(""TestXLM"")"
The Application.ExecuteExcel4Macro command that uses a variable inside the
RUN() function is more complex than the equivalent Application.Run method.
For the command to be properly evaluated, the macro string must be entered
as:
"RUN(""" & MacroToRun & """)"
This command is evaluated as:
RUN("" & MacroToRun & "")
which is a valid Microsoft Excel version 4.0 macro command.
Call TestVBSubNote that you cannot pass a variable name to the Call method. For example, if you have the variable "SubToRun" set to "TestVBSub," you cannot run the TestVBSub Sub procedure with the following:
Call SubToRun
TestVBSubon a line by itself. When that line in the subroutine is executed, it will run the TestVBSub subroutine.
A1: TestXLM
A2: =ALERT("TestXLM works!")
A3: =RETURN()
Option Explicit
'The TestVBSub subroutine displays a message box: it is the Visual
'Basic equivalent of the TestXLM macro shown above.
Sub TestVBSub()
MsgBox "TestVBSub works!" 'Displays a message box.
End Sub
'The Test1 Sub procedure makes use of the Application.Run method with
'hard-coded macro/subroutine names.
Sub Test1()
Application.Run ("TestVBSub")
Application.Run ("TestXLM")
End Sub
'The Test2 Sub procedure makes use of the Application.Run method with
'variable macro/Sub procedure names.
Sub Test2()
Dim SubToRun As String, MacroToRun As String
SubToRun = "TestVBSub"
MacroToRun = "TestXLM"
Application.Run (SubToRun)
Application.Run (MacroToRun)
End Sub
'The Test3 Sub procedure makes use of the
'Application.ExecuteExcel4Macro
'method with hard-coded macro/Sub procedure names.
Sub Test3()
'Note the extra quotation marks which are contained within the
'RUN statements. These are required in order for the command to
'evaluate properly.
Application.ExecuteExcel4Macro "RUN(""TestVBSub"")"
Application.ExecuteExcel4Macro "RUN(""TestXLM"")"
End Sub
'The Test4 Sub procedure makes use of the
'Application.ExecuteExcel4Macro
'method with variable macro/Sub procedure names.
Sub Test4()
Dim SubToRun As String, MacroToRun As String
SubToRun = "TestVBSub"
MacroToRun = "TestXLM"
'Note the extra quotation marks which are contained within the
'RUN statements. These are required in order for the command to
'evaluate properly.
Application.ExecuteExcel4Macro "RUN(""" & SubToRun & """)"
Application.ExecuteExcel4Macro "RUN(""" & MacroToRun & """)"
End Sub
'The Test5 Sub procedure uses the Call method with hard-coded
'Sub procedure names.
Sub Test5()
Call TestVBSub
End Sub
'The Test6 Sub procedure runs the TestVBSub subroutine because its
'name is entered on a line by itself.
Sub Test6()
TestVBSub
End Sub
Additional query words: 8.00 XL97 XL98 XL7 XL5 XL
Keywords: kbdtacode kbhowto kbprogramming KB108519