Hello, I want to prevent the user from accessing the excel spreadsheets.
therefore I disable the close command (the cross in the upper right corner to close a window) and ask him to use a cancel button.
What should happen when user exits the userform:
The user will then be asked if he really wants to close or not. There he can choose between OKAY and Cancel. If he presses okay he will exit the Userform and Excel will close automatically. However, If he presses Cancel he should stay on the userform.
When the user is asked if he "really wants to close" and presses the cancel button, excel stays open and the userform closes. BUT the userform should stay open and excel should be hidden....
Here is my code
In this workbook:
Private Sub Workbook_open()
Application.WindowState = xlMinimized 'user cannot see what is entered in Excel
AppActivate ("Microsoft Excel")
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If (MsgBox(prompt:=" Do you really want to close?", Buttons:=vbOKCancel) = vbCancel) Then
Cancel = True
Cancel = False
If Cancel = False Then
and in the UserForm I used this code for the cancel Button:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then Cancel = 1
MsgBox ("Please click the Cancel button to exit the program")
Private Sub CMD_Cancel_Click()
ActiveWorkbook.Close savechanges = True