Excel crashing after running workbook save, close vba code

dss28

Board Regular
Joined
Sep 3, 2020
Messages
165
Office Version
  1. 2007
Platform
  1. Windows
While executing the following code I always get a message that

"MS OFFICE EXCEL HAS STOPPED WORKING... ...... CHECK ONLINE FOR A SOLUTION...... CLOSE THE PROGRAM TO RESTART ......."

Request to guide

VBA Code:
Private Sub cmdLogout_Click()
    
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Application.Quit

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try to DELETE THIS LINE OF CODE
VBA Code:
Application.Quit
 
Upvote 0
thanks Mohadin

but the same thing is happening after deleting the line, otherwise the program is running smoothly without any issues.
 
Upvote 0
Where is that button? Sheet or userform? And is the active workbook the one with the code in it?
 
Upvote 0
Thanks RoryA

I have the code written in

1. this workbook and

2. in the userform or sheet where ever I have placed the button for logout.
 
Upvote 0
That really doesn't answer the question. Is the code you posted, which is causing the error, in a userform or a worksheet? If it's in a userform, you really ought to be unloading the form before you close the workbook. If the code is in the active workbook, the the application.quit line should never be reached anyway.
 
Upvote 0
Sorry RoryA if I am not able to properly explain or write

in my program there are few sheets and few userforms.

I have written this code in the "ThisWorkbook" (Developer -- Visual Basic -- VBA Project----"ThisWorkbook) and also in the few userforms in the same program from which the user may logout / close the program.
I also have the worksheets which are open to the user form working and from which they can log out. So I have provided them a Log out button on the sheets also.

Should I write the following code in the userform ?

VBA Code:
Private Sub cmdLogout_Click()
    
    Unload Me
    ActiveWorkbook.Save
    ActiveWorkbook.Close
   

End Sub

what should be the code if the user wants to close the program when they are viewing the particular worksheet in the program and click the log out button?

pl. help .. I tried to explain but dont know if it is clear...

thanking you ...
 
Upvote 0
At minimum, yes you should have Unload Me for the buttons in userforms, although ideally you would not have a form closing the workbook directly. It would hide, then whatever code called it would unload it and then do whatever else is required.

For buttons on worksheets, there shouldn't be an issue - you can't unload them other than by closing the workbook. Note: if you are closing the workbook that the code is in, you should use ThisWorkbook and not ActiveWorkbook.
 
Upvote 0
Changing from "ActiveWorkbook" to "ThisWorkbook." with additional "Unload Me" in userform resolved the userform close issue.

However log out from worksheet with command button linked to the code still the issue persists.
This code has only the Thisworkbook.Save" and "Thisworkbook.Close" code.

Is it possible that any of the form is still open in the background (although not visible) which may be causing this issue.
can you please suggest as usually I hide the previous userforms before going to the next step.
 
Upvote 0
It's certainly possible - I have no idea what your workbook does! ;)

As a general rule, code that loads a userform should take care of unloading it at the relevant moment.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,617
Latest member
Narendra Babu D

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top