Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to close down excel application not working properly

Status
Not open for further replies.

Frank Melan

Technical User
Feb 22, 2017
25
0
0
US
I am currently using VBA code to close excel, rather than several steps. Originally the code worked as expected. Now it closes but gives this error code. "Microsoft Excel has stopped working. A problem caused the program to stop working correctly. Please close the program. Here is the current codes. Code #1 closes excel when not on the home page.
Code:
Sub Exit_Referals()
Dim MsgBoxResult As Long

 MsgBoxResult = MsgBox("Would you like to Exit the Referral Workbook?" & vbCr, _
 vbYesNo)

 If MsgBoxResult = vbNo Then
 Exit Sub
 ElseIf MsgBoxResult = vbYes Then
 Sheets("TOC").Select
Application.Quit
Workbooks.Close
 End If
End Sub
Code #2 closes excel from the home page.
Code:
Sub TOC_Exit_Referals()

Dim MsgBoxResult As Long

 MsgBoxResult = MsgBox("Would you like to Exit the Referral Workbook?" & vbCr, _
 vbYesNo)

 If MsgBoxResult = vbNo Then
 Exit Sub
 ElseIf MsgBoxResult = vbYes Then
 Application.Quit
Workbooks.Close
 End If
End Sub
I am hoping that you Geniuses, can figure out what is happening & how to fix it. This old boy is at wits end with this particular batch of code.
 
How can you first quit excel and next close workbooks?
Quit application without closing workbooks, excel will do it for you. Some comments to your code:
- if you have unsaved workbooks, save them first or close individually without saving changes or set Saved property to True,
- if any of the workbooks has BeforeClose or other event procedure firing when you close workbook, the code will be called too,
- you do not have to select "TOC" sheet, it will not be saved unless ather code saves workbook later.

combo
 
I failed to mention that the normal save request comes up. If there has been new data entered, then save chosen & the data saved. TOC is used when excel is exited from any sheet other than TOC (Table of Contents). TOC is the home page for that particular excel app, & with multiple users, you want one common page to open too.
 
How many workbooks are closed? Are all saved (you can test it by adding End statement before Application.Quit and close workbooks manually after quitting procedure)?
You can remove Workbooks.Close from your code. What about my question about event procedures?


combo
 
Just one workbook is saved at a time. The procedures were added at user requests. I did look at what you mentioned prior to coming up with the current code. The code just started acting stupid recently. Up till last week there were no issues
 
Frank said:
The code just started acting stupid recently. Up till last week there were no issues

So what was changed/added in the code last week? Before the app started 'acting stupid'?

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
How are either Exit Referals procedures called?

Did you remove the Application.Quit statement from both procedures?

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Yes. There was no change. I did it on the home page (TOC).
 
I just learned something just now. The problem is only with one workbook. I am getting the impression that I may just have to live with the problem.
 
This is an update to the problem. It happens sporadically, so I will just have live with the issue. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top