XL5: "Cannot Find Macro" Running Macro in Active Workbook

ID: Q115905


The information in this article applies to:
  • Microsoft Excel for Windows, version 5.0


SYMPTOMS

In a Visual Basic macro in Microsoft Excel, if you activate a sheet in another workbook, and you use the Run method to run a macro in the activated workbook, such as SHEET1!Auto_Activate, you receive the following error message:

Run-time error '1004':

Cannot find macro 'SHEET1!Auto_Activate'.


CAUSE

This error occurs if you use the Run method to run a macro contained in another workbook, and you do not include the workbook name with the name of the macro you want to run. The error occurs because, by default, the Run method tries to find the macro name you specify within the workbook that contains the code that is running, not the active workbook.

For example, if you run a macro in BOOK1.XLS, that activates a sheet in BOOK2.XLS, and uses the Run method to run a macro contained in BOOK2.XLS, you must include the book name BOOK2.XLS with the macro name in the Run method command to run the macro.


STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Excel for Windows version 5.0c.


WORKAROUND

To avoid receiving this error message when you use the Run method to run a macro in a workbook other than the workbook that contains the current code, do either of the following:

  • Include the name of the book that contains the macro that you want to run, for example:
    
          Run "BOOK2.XLS!Macro_Name" 
    If you are running an auto macro contained on Sheet1, you can call the macro from another workbook with the following command:
    
          Run "[BOOK2.XLS]Sheet1!Auto_Activate" 
    NOTE: An Auto_Activate auto macro defined on a worksheet does not run when the sheet is activated by a Visual Basic procedure.


  • -or-

  • Use the ActiveWorkbook property to run the macro in the active workbook. This causes the Run method to search for the macro in the active workbook rather than the workbook that contains the code that is currently running. The following is an example of using this method:
    
          Run "'" & ActiveWorkbook.Name & "'!Macro_Name" 


Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.


REFERENCES

For more information about the ActiveWorkbook Property, choose the Search button in the Visual Basic Reference and type:

ActiveWorkbook Property


For more information about the Run Method, choose the Search button in the Visual Basic Reference and type:

Run Method

Additional query words: 1.00 demand load err msg

Keywords : xlwin
Version : 5.00
Platform : WINDOWS
Issue type :


Last Reviewed: July 23, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.