"ThisWorkbook.Close" Causes excel to crash

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

ThisWorkbook.Close savechanges:=True 

This leads to the fact that 2 out of 4 supported computers can distort EXCEL (Windows XP = OK, Windows 10 = OK 1 OTHER OTHER, Windows 8 = BAD).

enter image description here

I highlighted the incident with this particular line of code (I made a file with 1 excel sheet with only a close button, and it still crashes) I noticed that if the excel file is not the only one open, sometimes it does not crash (maybe the problem is closing the file itself excel)

What I did separates 2 statements, so if (when) it works, it is already saved:

 ThisWorkbook.Save ThisWorkbook.Close 

Can anyone shed some light? I am really lost. I tried all the alternatives that I could come up with (activeworkbook ...)

Tl; dr: "ThisWorkbook.Close" Calls excel to fail

+5
source share
1 answer

This is a standard error in Microsoft Excel. Not sure if Microsoft has any fixes. However, there are workarounds to solve this problem.

This problem occurs when the Close event is fired from the click event, but works fine with another event, such as Change Change. To solve this problem, you can try the following:

Add the following code to the button click event:

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

In the standard module add the following code

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

This works for me. Let me know if this is helpful.

+7
source

Source: https://habr.com/ru/post/1259537/


All Articles