excel vba 创建登录屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6090838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
excel vba creating login screen
提问by bdubb
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.
并提前感谢您的帮助。我正在为某些工作簿创建登录屏幕 在打开此工作簿时,我有“userform1.show”,并且在用户表单中我已经硬编码了用户名和密码。当 userform1 弹出询问用户名和密码时,我可以单击 X 并且它只是关闭表单并且用户仍然可以使用工作簿。我如何让 X 关闭整个工作簿。我不知道那个 X 在 VB 中叫什么。我已经尝试了所有 3 个选项的“application.enablecancelkey”,但没有任何效果。
A) is that the right way to go about it? B) if it is where do I put that?
A) 这是正确的方法吗?B)如果它是我把它放在哪里?
回答by Oneide
You must set the QueryClose event up like this:
您必须像这样设置 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
Please, make the adjustments you think it's needed to better suit your real case.
请进行您认为需要的调整,以更好地适应您的实际情况。
回答by skofgar
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"
或者,您也可以防止登录表单被“ 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...
这就是我们如何通过登录来做到这一点...