EXCEL Illegal Operation.


Posted by Michele on September 10, 2001 2:37 AM

I have an "EXIT" button in Sheet 1 and I have the following code for this button:

Private Sub CommandButton2_Click()
' Exit Button
Sheets("Sheet1").Activate
Range("B1").Select
ActiveWorkbook.Close
End Sub

I also have the following code in the Worksheet Module and I presume it also gets executed upon clicking the EXIT button.
Private Sub Workbook_Deactivate()
Application.CommandBars(1).Enabled = True
Application.CommandBars("Worksheet Menu Bar").Enabled = True
Application.CommandBars("Chart Menu Bar").Enabled = True
Application.CommandBars("Standard").Visible = True
Application.CommandBars("Formatting").Visible = True
Application.DisplayFormulaBar = True
ActiveWindow.DisplayHeadings = True
Range("A1").Select
Range("B1").Select
Beep
End Sub

Upon clicking the Exit button, many times I end up with the message:
Title = Excel
Message = "This program has performed an illegal operation and will be shut down."
with "Close" and "Deatils" buttons.
The "Details" button gives:
"EXCEL caused an invalid page fault in Module EXCEL.EXE at 0167:3006ad15.
Registers
EAX=0000de08 CS=0167 EIP=3006ad15 EFLGS=000102216
EBX=004704dc"....etc.

By the way, I used to get this sort of problem with Lotus as well so I don't think it's my code that is causing this.

Does anyone know what the problem/solution is?

Thanks,
Michele

Posted by Juan Pablo on September 10, 2001 6:57 AM

I think the problem is that the in this lines in the worbook_deactivate() sub

Range("A1").Select
Range("B1").Select

Just delete this two (Because your workbook is already closed) and i think that would solve the problem.

Juan Pablo



Posted by Damon Ostrander on September 10, 2001 7:17 AM

Hi Michele,

The workbook's Deactivate event does not ordinarily run upon closing the workbook. Of course, you can check this by putting a breakpoint in the Deactivate routine.

There is an obvious problem here, however. Your code assumes that the Deactivate event triggers BEFORE the workbook is deactivated. Actually it triggers AFTER, so any references to workbook objects in the Deactivate event will actually be referring to another workbook. If no other workbook is open in Excel, an error can be expected.

If you do want this code to run upon closing the workbook, I suggest you put it in the BeforeClose event, which ensures that the workbook is still there at the time the code runs.

There are also several redundant lines of code. For example, there is no use in selecting A1, then selecting B1 which deselects A1.

I hope this helps.

Damon