I created a password protected excel using VBA which only show limited/full access of sheets base on password input.
I made it work somehow, but the problem is if the user closes the form instead of inputting the password, they will be able to access the excel.
is there a that if the user closes the form using the "X" icon at the top right corner it will close the excel or workbook?
Thank you
Use the form's QueryClose
or Terminate
event.
QueryClose
gives you the option of canceling the closing based on your code.
Application.DisplayAlerts = False
Application.Quit
Here's an example of cancelling it:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If MsgBox("Are you sure you want to close Excel?", vbQuestion Or vbYesNo) = vbYes Then
Application.DisplayAlerts = False
Application.Quit
Else
Cancel = True
End If
End Sub
Note: Keep in mind that there is no way to 100% prevent the user from "accessing the excel"