XL97: Cannot Stop Macro After Switching to Other Program

Last reviewed: March 13, 1998
Article ID: Q163497
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SYMPTOMS

When you run a Visual Basic for Applications macro in Microsoft Excel 97, the following problems may occur:

  • You cannot stop the execution of the macro by pressing ESC or CTRL+BREAK.

    -and-

  • The screen in Microsoft Excel 97 is not updated until the macro is finished running. As a result, Microsoft Excel 97 appears to stop responding.

CAUSE

This problem may occur when the following conditions are true:

  • The Visual Basic macro requires a long time to finish running.

    This may be true if your macro contains loops that iterate many times.

    -and-

  • While the macro is running, you switch to any other program, such as Microsoft Notepad or Microsoft Word 97.

    -and-

  • You then attempt to switch back to Microsoft Excel 97.

WORKAROUND

To work around this problem, wait for the macro to finish running. After the macro is finished, Microsoft Excel 97 updates the screen. If you wait for a long time and the macro does not finish running, It may be necessary to end the instance of Microsoft Excel 97 by pressing CTRL+ALT+DEL.

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 engineers 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.

Although it is not possible to prevent this problem from ever occurring, you can lessen its affects by adding MsgBox commands to the Visual Basic macro. For example, assume you are running the following Visual Basic macro:

   Sub Test()
       For X = 1 To 4000
           ThisWorkbook.Worksheets(1).Cells(1, 1).Value = X
       Next X
       For Y = 1 To 4000
           ThisWorkbook.Worksheets(1).Cells(1, 2).Value = Y
       Next Y
       For Z = 1 To 4000
           ThisWorkbook.Worksheets(1).Cells(1, 3).Value = Z
       Next Z
   End Sub

When you run this macro, switch to another program, and then switch back to Microsoft Excel 97, you may not be able to halt the macro by pressing ESC. Also, the screen may not be updated properly until the macro is finished. However, you can add message boxes that allow the screen to be updated. For example, type the following subroutine:

   Sub Test()
       For X = 1 To 4000
           ThisWorkbook.Worksheets(1).Cells(1, 1).Value = X
       Next X
       MsgBox "Stage one complete!"     'new line of code
       For Y = 1 To 4000
           ThisWorkbook.Worksheets(1).Cells(1, 2).Value = Y
       Next Y
       MsgBox "Stage two complete!"     'new line of code
       For Z = 1 To 4000
           ThisWorkbook.Worksheets(1).Cells(1, 3).Value = Z
       Next Z
       MsgBox "Stage three complete!"   'new line of code
   End Sub

When each loop is completed, the macro displays a message box. When you click OK, the screen is updated properly, and you can halt the macro by pressing ESC.

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.

MORE INFORMATION

This problem occurs even if the Visual Basic macro contains one of the following lines of code:

   Application.EnableCancelKey = xlInterrupt

   -or-

   Application.EnableCancelKey = xlErrorHandler


Additional query words: 97 XL97 hang freeze stop
Keywords : kbprg
Version : WINDOWS:97
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: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.