SYMPTOMS
When you click a control from the
Forms toolbar (such as a Button) to run a Microsoft Visual Basic for Applications macro, and that macro uses the
Close method, 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:300e7e53.
WORKAROUND
To close the active workbook without receiving the error message, use either of the following methods.
Method 1: Use an ActiveX Control Instead of a Forms Control
Instead of running your macro with a control from the
Forms toolbar, consider using an ActiveX control instead. For example, if you currently use a Button control from the
Forms toolbar, use a CommandButton control (which is an ActiveX control) from the
Control Toolbox instead.
To attach your current code to the Click event of the ActiveX control, double-click the command button. In the CommandButton1_Click event procedure, copy your code from the Forms button.
Method 2: Select a Cell
Select a cell on the worksheet before you click the Forms control that runs your macro. This removes the focus from the ActiveX control, allowing the workbook to close properly. You can either select a cell using your mouse or use a macro assigned to the ActiveX control's Click event to select a specified cell for you.
To select a cell in a macro, add a statement similar to either of the following in the Click event procedure of your ActiveX control.
ActiveCell.Select
Range("A1").Select