All, I have a problem. I want to have the user to select whether they want to save the file.
This is done via a message box with YES (save and exit workbook), NO (exit workbook), CANCEL (do nothing).
However, for some reason the message box buttons need to be clicked twice to close the workbook.
The code I am using is as follows:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim gui As Worksheet
Dim optimiser As Shape
Set gui = ThisWorkbook.Sheets("GUI")
Set optimiser = gui.Shapes("Commandbutton2")
Application.DisplayAlerts = False
For Each Sht In Application.Worksheets
Sht.Sort.SortFields.Clear
Next Sht
gui.Shapes("Check Box 157").OLEFormat.Object.Value = -4146
gui.Shapes("Check Box 88").OLEFormat.Object.Value = -4146
Call Exit_Program
End Sub
Sub Exit_Program()
Dim Answer As String
Dim Question As String
Question = "All analysis input data will be lost. Do you want to save your file before you exit?"
Answer = MsgBox(Question, vbYesNoCancel)
If Not ThisWorkbook.Saved Then
Select Case Answer
Case vbYes
ThisWorkbook.Save
Case vbNo
ThisWorkbook.Saved = True
Case vbCancel
Cancel = True
Exit Sub
End Select
End If
ThisWorkbook.Close
End Sub
What am I doing wrong?
Regards,
Martin
Bookmarks