excel vba创buildlogin屏幕

并提前感谢帮助。 我正在创build一些工作簿的login屏幕在此工作簿正在打开我有“userform1.show”,并在用户表单我硬编码的用户名和密码。 当userform1popup询问用户名和密码时,我可以点击X,它只是closures窗体,用户仍然可以使用工作簿。 我如何让Xclosures整个工作簿。 我不知道在VB中调用X. 我已经尝试了所有3个选项“application.enablecancelkey”,但没有任何工作。

一)是正确的方式去呢? B)如果是我在哪里放?

你可以写

Private Sub UserForm_QueryClose(cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then ThisWorkbook.Close End If End If 

或者,您也可以阻止login表单被“ 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 

这就是我们的login方式

您必须像这样设置QueryClose事件:

 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 

请进行调整,您认为需要更好地适应您的真实情况。