Excel crashing after running workbook save, close vba code

dss28

Board Regular
Joined
Sep 3, 2020
Messages
61
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
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
Try to DELETE THIS LINE OF CODE
VBA Code:
Application.Quit
 

dss28

Board Regular
Joined
Sep 3, 2020
Messages
61
Office Version
  1. 2007
Platform
  1. Windows
thanks Mohadin

but the same thing is happening after deleting the line, otherwise the program is running smoothly without any issues.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Where is that button? Sheet or userform? And is the active workbook the one with the code in it?
 

dss28

Board Regular
Joined
Sep 3, 2020
Messages
61
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

dss28

Board Regular
Joined
Sep 3, 2020
Messages
61
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

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 ...
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

dss28

Board Regular
Joined
Sep 3, 2020
Messages
61
Office Version
  1. 2007
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,570
Messages
5,625,579
Members
416,119
Latest member
JCLLE

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
Top