and thanks in advance for helping. I am creating a log in screen for some workbooks Upon thisworkbook being open I have "userform1.show" 开发者_如何学运维and in the userform i have hardcoded a username and password. when the userform1 pops up asking for username and password I am able to click the X and it just closes the form and the user can still use the workbook. How do I have that X close the entire workbook. I don't know what that X is called in VB. I have tried "application.enablecancelkey" with all 3 options but none work.
A) is that the right way to go about it? B) if it is where do I put that?
You must set the QueryClose event up like this:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If MsgBox("Exit?", vbOKCancel) = vbOK Then
ThisWorkbook.Close
Else
Cancel = True
End If
End Sub
Please, make the adjustments you think it's needed to better suit your real case.
You could write
Private Sub UserForm_QueryClose(cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
ThisWorkbook.Close
End If
End If
Alternatively you could also prevent the Login Form being closed by the "X"
Private Sub UserForm_QueryClose(cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
cancel = 1
MsgBox "Please use cancel if you want to leave the Login Box.", _
vbOKOnly + vbInformation, "Please confirm ..."
End If
End Sub
That's how we do it with our logins...
精彩评论