XL: Error Running Inserted Text File Macro from Another Macro

Last reviewed: February 3, 1998
Article ID: Q117363
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for the Macintosh, versions 5.0, 50.a
  • Microsoft Excel 98 Macintosh Edition

SYMPTOMS

If you run a Microsoft Visual Basic for Applications procedure that inserts a macro as a text file into a module, and then runs the macro in that module, you receive the following error message:

   Run-time error '1004':

   Cannot find macro 'MacroName'

CAUSE

This error appears if either of the following is true:

  • You insert the macro into an existing module. (This is the case if you have already inserted a macro as a text file into the same module.)

        -or-
    
  • You have the Require Variable Declaration option selected.

The error occurs because when you run a procedure that inserts a macro as a text file into a module that has already been compiled, the existing module is not decompiled to recognize the new macro until the procedure that inserts the macro is finished.

Note that because of this behavior, it is possible to run a procedure that inserts two macros with the same name into the same module, and then runs the macro inserted first, because the second macro is not recognized until the procedure that inserts the two macros is finished.

The error occurs in the second case because the macro is inserted above the Option Explicit statement. The Option Explicit statement must appear at the beginning of a module.

WORKAROUNDS

To avoid this error message when you run a Visual Basic procedure to insert an entire macro into a new module, and then run the macro, do the following:

  • Insert the macro as a text file into a new module. Use the Add method of the Modules object to add a new module to the workbook before inserting the macro.

        -and-
    
  • Clear the Require Variable Declaration check box on the Module General tab of the Options dialog box. This option adds the Option Explicit statement automatically when you create a new module.

If you are inserting more than one macro into the module, do either of the following:
  • Combine the macros in one text file. You cannot add them as separate text files into the same module.

        -or-
    
  • Insert each text file macro into a new module.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.


Additional query words: 5.00 5.00a 5.00c 7.00 err msg
Keywords : kberrmsg kbprg PgmOthr
Version : WINDOWS:5.0,5.0c,7.0,7.0a,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbbug
Solution Type : kbworkaround


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.