Quiting Excel program?

T

T.c.Goosen1977

Im using the following code to quite excel:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
Application.Quit
ActiveWorkbook.Close
End Sub

A standard saving message pops up when i click the "x" on the userform,
if I click “yes” it saves the file and quits excel = 100% , if I click
“no” it does not save the file but it quits excel = 100%, but when I
click the cancel button or the "x" it does not unload the standard
excel saving message and resume the userform...it goes into debug mode?
 
L

leonidas

Hi Theuns,

Try this piece of VBA code. In the VB Editor you will find the folder
"Microsoft Excel Objects". Put this code in the object "ThisWorkbook"
and it should work fine!


Code:
--------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)

If Not Me.Saved Then
Msg = "Do you want to save the changes you made to "
Msg = Msg & Me.Name & "?"
Ans = MsgBox(Msg, vbQuestion + vbYesNoCancel)
Select Case Ans
Case vbYes
Me.Save
Case vbNo
Me.Saved = True
Case vbCancel
Cancel = True
Exit Sub
End Select
End If

End Sub
 
G

Guest

The procedure has been "blocked" during the file save process.
To solve this problem, you need to do one more step, which is, after Excel
prompts to save all unsaved files, use macro to force Excel to close all
workbooks no matter what. Then quit application.

The code below may look longer than necessary. But it'll ensure a more
secured result.

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim a As Workbook
For Each a In Workbooks
If a.Name <> ThisWorkbook.Name Then a.Close
Next
For Each a In Workbooks
If a.Name <> ThisWorkbook.Name Then a.Close False
Next
ThisWorkbook.Saved = True
Application.Quit
End Sub

Regards,
Edwin Tam
(e-mail address removed)
http://www.vonixx.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top