XL: Running Subroutines and Macros from Visual BasicLast reviewed: February 3, 1998Article ID: Q108519 |
The information in this article applies to:
SUMMARYIn 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.
MORE INFORMATIONMicrosoft 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 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/refguide/default.asp 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:
Application.Run ("TestXLM")If you have the variable "MacroToRun" set to "TestXLM," you could use this method:
Application.Run (MacroToRun) 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:
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.
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:
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 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
TestVBSubon 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:
|
Additional query words: 5.00 7.00 8.00 XL97 XL98 XL7 XL5
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |