Running Subroutines and Macros from Visual Basic (108519)



The information in this article applies to:

  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

This article was previously published under Q108519

SUMMARY

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.

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 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 Method

You 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 Method

The 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 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 the TestVBSub 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 a line by itself. For example, if you want your Sub procedure to run the TestVBSub subroutine, you would enter
   TestVBSub
				
on a line by itself. When that line in the subroutine is executed, it will run the TestVBSub subroutine.

Sample Visual Basic Procedures

To 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:

  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 appear with the messages "TestVBSub works!" and "TestXLM works!" When you run Test5 or Test6, one alert box will appear with the message "TestVBSub works!"

Modification Type:MinorLast Reviewed:10/10/2006
Keywords:kbdtacode kbhowto kbProgramming KB108519