“ThisWorkbook.Close” Causes excel to crash

2019-06-22 14:26发布

问题:

I have a large collection of excel files that work almost like a program, and they run on multiple computers (with different versions of windows), and as of latelly I have this bad problem, when the user presses my close button(actually a picture I associate a macro with), with the code calls:

ThisWorkbook.Close savechanges:=True

It causes 2 of the 4 supported computers to crach EXCEL (Windows XP = OK, Windows 10 = OK 1 BAD OTHER, Windows 8 = BAD).

I have isolated the incident to this particular line of code (made a 1 sheet excel file with just a close button, and it still crashes) I have noted that if the excel file isn't the only one open, sometimes it doesn't crash (maybe the problem is with closing excel itself)

What I have done is separeted the 2 statements so if (when) it crashes it's already saved:

ThisWorkbook.Save
ThisWorkbook.Close

Can anyone shed some lights? I'm really lost. I tried all the alternatives I could think off (activeworkbook...)

Tl;dr: "ThisWorkbook.Close" Causes excel to crash

回答1:

This is a standard bug in Microsoft Excel. Not sure if Microsoft has any fix. However, there are workarounds to overcome this issue.

This issue occurs when "Close" event is triggered from a click event but works fine with other event like "Selection Change". To tackle this issue, you may try this one:

Add the following code in the click event of the button:

Private Sub CloseButton_Click()
    Cancel = True
    Application.OnTime Now, "Close_Xls"
End Sub

In a standard module, add the following code

Sub Close_Xls()
   ThisWorkbook.Close  savechanges:=True
End Sub

It works for me. Let me know if it is helpful