Article ID: 213837 - View products that this article applies to.
This article was previously published under Q213837
For a Microsoft Excel 97 and earlier version of this article, see 108519
In Microsoft Excel, you can run Microsoft Visual Basic for Applications Sub procedures and Microsoft Excel version 4.0 macros from a Visual Basic procedure by using the Application.Run and Application.ExecuteExcel4Macro methods. You can also run Visual Basic Sub procedures with the Call method or by entering the name of a procedure on a line by itself.
This article illustrates several methods that you can use to run Sub procedures and Microsoft Excel version 4.0 macros from Visual Basic in Microsoft Excel.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners - https://partner.microsoft.com/global/30000104
Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
Method 1: Application.RunYou can use the Application.Run method to run Visual Basic Sub procedures or Microsoft Excel version 4.0 macros from other Visual Basic procedures. The Application.Run method requires one named argument: the name of the macro or Sub procedure to be run. (However, other optional arguments may also be included.) This name can be a text string (for example, "TestXLM") or it can be a variable that is equal to the name of the macro.
Case 1: Text StringTo run a Microsoft Excel version 4.0 macro called TestXLM, you could use this text string:
Case 2: VariableIf you have the variable "MacroToRun" set to "TestXLM," you could use this variable:
Method 2: Application.ExecuteExcel4MacroYou can also use the Application.ExecuteExcel4Macro method to run Microsoft Excel version 4.0 macros or other Visual Basic Sub procedures, but the syntax is somewhat different. To use Application.ExecuteExcel4Macro to run a macro or Sub procedure, you must also include the Microsoft Excel version 4.0 RUN() function, as in the following examples:
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:
The entire string must then be enclosed in quotation marks. When you add quotation marks to the outside of the string, you must add an additional quotation mark adjacent to each quotation mark within the string:
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:
This command is evaluated as
which is a valid Microsoft Excel version 4.0 macro command.
Method 3: The Call MethodThe Call method may be used to run Visual Basic Sub procedures, but not Microsoft Excel version 4.0 macros. For example, to run the Sub procedure TestVBSub, you would use this method:
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:
Method 4: Run a Sub Procedure Using Only Its NameYou can run a Visual Basic Sub procedure by entering its name on a line by itself. For example, if you want your Sub procedure to run the TestVBSub subroutine, you would enter
on a line by itself. When that line in the subroutine is executed, it will run the TestVBSub subroutine.
Method 5: Sample Visual Basic ProceduresTo create six Sub procedures that illustrate the most common methods you can use to run a Visual Basic Sub procedure or Microsoft Excel version 4.0 macro from another Visual Basic procedure, follow these steps: