XL2000: Crash Running Macro When Object Set to Nothing

ID: Q235107


The information in this article applies to:
  • Microsoft Excel 2000


SYMPTOMS

In Microsoft Excel, when you run a Microsoft Visual Basic for Applications macro, you may receive the following error message:

This program has performed an illegal operation and will be shut down.
If you click Details, you receive an error message similar to the following:
EXCEL caused an invalid page fault in module EXCEL.EXE at 015f:300603c4.
This may be followed by a second illegal operation error message similar to the following:
EXCEL caused an invalid page fault in module EXCEL.EXE at 015f:300149b0.
NOTE: You may receive this error message even though the procedure ran successfully in earlier versions of Microsoft Excel.


CAUSE

This problem may occur if all of the following conditions are true:

  • You create an object variable in a Visual Basic for Applications macro and assign it to an object.

    -and-

  • When you are finished using the object variable, you set the object variable to the Nothing keyword, so the variable no longer refers to the actual object.

    -and-

  • You run the macro.
NOTE: If you step through the macro, it does not crash.


RESOLUTION

Microsoft provides programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
To prevent this problem, explicitly close an object with the Close method before setting an object variable to Nothing.

Example:

The following code may generate the errors shown in the "Symptoms" section:

Sub Test()
Set x = CreateObject("Excel.Sheet")

Set y = Workbooks.Add
x.Activate

Debug.Print y.Author

Set x = Nothing
End Sub 
Replace this with:

Sub Test()
Set x = CreateObject("Excel.Sheet")

Set y = Workbooks.Add
x.Activate

Debug.Print y.Author
x.Close
Set x = Nothing
End Sub 


STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

It is generally good programming practice to close an object you have created before destroying it.

Additional query words: XL2000 crash crashes crashing crashed quit quitting quits fail fails failing failed break

Keywords : kbdta kbdtacode OffVBA KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbbug


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