Article ID: 108519 - View products that this article applies to.
This article was previously published under Q108519
In Microsoft Excel versions 5.0 and later, 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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.
To Use the Application.Run MethodYou 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.
For example, to run a Microsoft Excel version 4.0 macro called TestXLM, you could use this method:
If you have the variable "MacroToRun" set to "TestXLM," you could use this method:
To Use the Application.ExecuteExcel4Macro MethodYou 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:
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:
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:
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.
To Use 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:
Note 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:
To Run a Sub Procedure Using Only Its NameYou can also 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.
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:
Article ID: 108519 - Last Review: October 10, 2006 - Revision: 2.2
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Contact us for more help