I'm now trying to adapt that code to allow three options:
- Close both form and workbook if Yes is clicked;
- Close only the form if No is clicked;
- Do nothing at all if Close is clicked.
Code: Select all
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
On Error Resume Next
Set wbk = Workbooks("Personal.xlsb")
On Error Resume Next
If MsgBox("Do you want to quit Excel as well?", vbYesNoCancel + vbDefaultButton2 + _
vbQuestion, "Quit Excel") = vbCancel Then GoTo Here
If vbNo Then
HideForm
Exit Sub
End If
If wbk Is Nothing Then
cnt = 1
Else: cnt = 2
End If
If Application.Workbooks.Count = cnt Then
Application.Quit
Else: ThisWorkbook.Close
End If
Exit Sub
Here: MsgBox "You're here"
ShowForm
End Sub
Secondly, this seems like an awkward way to go about this. Is there any way to simply cancel the close button, if cancel is clicked?