You are currently offline, waiting for your internet to reconnect

Running Subroutines and Macros from Visual Basic

This article was previously published under Q108519
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.
SUMMARY
In Microsoft Excel versions 5.0 and later, you can run Microsoft VisualBasic for Applications Sub procedures and Microsoft Excel version 4.0macros from a Visual Basic procedure by using the Application.Run andApplication.ExecuteExcel4Macro methods. You can also run Visual Basic Subprocedures with the Call method or by entering the name of a procedure ona line by itself.

This article illustrates several methods that you can use to run Subprocedures and Microsoft Excel version 4.0 macros from Visual Basic inMicrosoft Excel.
MORE INFORMATION
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 Method

You can use the Application.Run method to run Visual Basic Sub proceduresor Microsoft Excel version 4.0 macros from other Visual Basic procedures.The Application.Run method requires one named argument: the name of themacro or Sub procedure to be run. (However, other optional arguments mayalso 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 thismethod:
Application.Run (MacroToRun)

To Use the Application.ExecuteExcel4Macro Method

You can also use the Application.ExecuteExcel4Macro method to run MicrosoftExcel version 4.0 macros or other Visual Basic Sub procedures, but thesyntax is somewhat different. To use Application.ExecuteExcel4Macro to runa macro or Sub procedure, you must also include the Microsoft Excelversion 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 usequotation marks. For example, to use the RUN() function, you must enclosethe name of the argument in quotation marks:
   RUN("TestXLM")				
Because the entire string must also be enclosed in quotation marks, whenyou add quotation marks to the outside of the string, you must also add anadditional quotation mark adjacent to each quotation mark within thestring. The resulting string is as follows:
   "RUN(""TestXLM"")"				
The Application.ExecuteExcel4Macro command that uses a variable inside theRUN() function is more complex than the equivalent Application.Run method.For the command to be properly evaluated, the macro string must be enteredas:
   "RUN(""" & MacroToRun & """)"				
This command is evaluated as:
   RUN("" & MacroToRun & "")				
which is a valid Microsoft Excel version 4.0 macro command.

To Use the Call Method

The Call method may be used to run Visual Basic Sub procedures, but notMicrosoft Excel version 4.0 macros. For example, to run the Sub procedureTestVBSub, you would use this method:
   Call TestVBSub				
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 theTestVBSub Sub procedure with the following:
   Call SubToRun				

To Run a Sub Procedure Using Only Its Name

You can also run a Visual Basic Sub procedure by entering its name on aline by itself. For example, if you want your Sub procedure to run theTestVBSub subroutine, you would enter
   TestVBSub				
on a line by itself. When that line in the subroutine is executed, it willrun the TestVBSub subroutine.

Sample Visual Basic Procedures

To create six Sub procedures that illustrate the most common methods youcan use to run a Visual Basic Sub procedure or Microsoft Excel version 4.0macro from another Visual Basic procedure, follow these steps:

  1. In a new workbook, insert a Microsoft Excel 4.0 macro sheet called Macro1 and a Visual Basic module called Module1.

    To insert a Visual Basic module in Microsoft Excel 97 or Microsoft Excel 98, press ALT+F11 to activate the Visual Basic Editor. Then, click Module on the Insert menu.
  2. On the macro sheet, enter the following macro:
    A1: TestXLM
    A2: =ALERT("TestXLM works!")
    A3: =RETURN()
    This macro displays an alert box.
  3. On the macro sheet, select cell A1.
  4. On the Insert menu, point to Name, and then click Define.
  5. Verify that the following information appears in the Define Name dialog box:

    • The Names In Workbook box contains the name TestXLM.
    • The Refers To box contains the reference =Macro1!$A$1.
    • The Command option is selected under Macro.
    When the settings are as specified above, click OK to define the name of the macro.
  6. In Module1, enter the following Sub procedures:
          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						
When you run Test1, Test2, Test3, or Test4, two alert boxes will appearwith the messages "TestVBSub works!" and "TestXLM works!" When you runTest5 or Test6, one alert box will appear with the message "TestVBSubworks!"
8.00 XL97 XL98 XL7 XL5 XL
Properties

Article ID: 108519 - Last Review: 10/10/2006 18:05:36 - Revision: 2.2

  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 98 for Macintosh
  • kbdtacode kbhowto kbprogramming KB108519
Feedback