PDA

View Full Version : Solved: Close Excel Command Button



f2e4
08-25-2009, 09:02 AM
Hey guys,

I have a spreadsheet with a simple userform - Userform1.

On this userform there is 2 command buttons - one for closing excel immediately and one for saving the workbook and the closing excel. Code is as follows:

Private Sub CommandButton1_Click()
'CLOSE WORKBOOK WITHOUT SAVING
Unload Me
UserForm1.Hide
Application.Quit
End Sub

Private Sub CommandButton7_Click()
'CLOSE WORKBOOK AND SAVE
Unload Me
UserForm1.Hide
ThisWorkbook.Save
Application.Quit
End Sub


However, everytime I run the code, excel does close down but I keep getting the error:

'Excel has encountered a problem and needs to close'

Is there a more savvy way of writing the above processes to prevent excel from producing this error.

As always, your help is greatly appreciated.

Mario62
08-25-2009, 10:58 AM
Hi,

have you tried



Private Sub CommandButton1_Click()
Application.Quit
End Sub

Private Sub CommandButton7_Click()
ThisWorkbook.Save
Application.Quit
End Sub


I would not place hide
and unload me

Mario

GTO
08-25-2009, 02:10 PM
Greetings,

I couldn't seem to replicate the error and not well tested, but maybe:

Option Explicit

Private Sub CommandButton1_Click()
'CLOSE WORKBOOK WITHOUT SAVING
Unload Me
'// If only open wb//
If Workbooks.Count = 1 Then
'// Mark wb as Saved
ThisWorkbook.Saved = True
Application.Quit
Else
'// Else other wb's are open
ThisWorkbook.Close False
End If
End Sub

Private Sub CommandButton7_Click()
'CLOSE WORKBOOK AND SAVE
Unload Me
If Workbooks.Count = 1 Then
ThisWorkbook.Save
Application.Quit
Else
ThisWorkbook.Close True
End If
End Sub


Hope that helps,

Mark