XL5: Drag to Replace in Recorded Macro Returns Invalid Code

Last reviewed: September 2, 1997
Article ID: Q110709

The information in this article applies to:

- Microsoft Excel for Windows, version 5.0

SUMMARY

In Microsoft Excel version 5.0 for Windows, when you are recording a Visual Basic subroutine and you drag a cell or range of cells to another cell or range of cells to replace the original information, the recorded subroutine may contain syntax errors that prevent it from functioning correctly.

WORKAROUND

To work around this problem, you may need to modify the recorded code. The modified code should resemble the following:

   Sub Macro1()
      Range("A1").Select
      Selection.Cut (Range("B1"))
      Range("B1").Select
   End Sub

The third line, which originally contained invalid code, has been replaced by a line of code that performs the correct action.

MORE INFORMATION

Steps to Reproduce Problem

  1. In a new workbook, create a new worksheet and enter the following values:

            A1: Alpha   B1: Bravo
    
    

  2. From the Tools menu, choose Record Macro, and then choose Record New Macro.

  3. In the Record New Macro dialog box, choose the Options button. In the Expanded Record New Macro dialog box, Under Language, verify that the Visual Basic option is selected. Choose OK to begin recording.

  4. Select the contents of cell A1 and drag it to cell B1.

  5. When you are prompted with the "Replace contents of destination cells?" message, choose OK.

  6. To stop the Macro Recorder, choose Record Macro from the Tools menu, and then choose Stop Recording.

  7. Switch to the new Visual Basic module.

The following subroutine should be contained in the module:

   Sub Macro1()
      Range("A1").Select
   ExecuteExcel4Macro "CUT(Range("A1"),Range("B1"))"
      Range("B1").Select
   End Sub

The third line (which starts with the ExecuteExcel4Macro method) will appear in red, indicating that the line contains a syntax error.

  1. Switch to the worksheet.

  2. In cell A1, type "Charlie" (without the quotation marks).

  3. With cell A1 selected, choose Macro from the Tools menu.

  4. From the list of macros, select Macro1 and choose the Run button.

You will receive the error message "Syntax error" and the third line of the subroutine will be selected.


Additional query words: 5.00
Keywords : kbprg PgmOthr kbprg
Version : 5.00
Platform : WINDOWS


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: September 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.