Article ID: 213837 - Last Review: January 24, 2007 - Revision: 3.7 How to run subroutines and macros from Visual Basic in Excel 2000 or Excel 2002This article was previously published under Q213837 For a Microsoft Excel 97 and earlier version of this article, see 108519 (http://support.microsoft.com/kb/108519/EN-US/ ) . On This PageSUMMARY 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. 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 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 (https://partner.microsoft.com/global/30000104) Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (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 (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:-or- 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: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 enterMethod 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:
| Article Translations
|
Back to the top
