【问题标题】:How to check for InputBox exit (X) vs empty string如何检查 InputBox 退出(X)与空字符串
【发布时间】:2016-08-18 19:29:05
【问题描述】:

我有一个命令按钮,它启动一个输入框以允许输入密码。

Private Sub CommandButton1_Click()  
     Dim ThePW As String

     ThePW = InputBox("A password is required to run this procedure." & vbCrLf & _
        "Please enter the password:", "Password")

     If ThePW <> "123" Then MsgBox ("wrong pw")
End Sub

但是,当我点击退出(X)或取消时,消息框仍然显示wrong pw

我该如何解决这个问题?当我点击退出(X)或选择取消时,我不希望它显示wrong pw

【问题讨论】:

  • 试试If ThePW = "" then 'whatever else If ThePW &lt;&gt; "123"
  • 在“123”下面可以加Else,表示密码正确
  • @evabb 你在我下面的答案中测试过代码吗?
  • Trouble with InputBoxes的可能重复

标签: vba excel


【解决方案1】:

使用下面的代码,如果他点击取消,它就会退出Sub

Private Sub CommandButton1_Click()

Dim ThePW As String

ThePW = InputBox("A password is required to run this procedure." & vbCrLf & _
"Please enter the password:", "Password")

If ThePW = "" Then Exit Sub ' Exit if null input or cancel

If ThePW <> "123" Then
    MsgBox ("wrong pw")
Else
    ' do something if OK
End If        

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-05
    • 2011-07-26
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 2012-03-23
    相关资源
    最近更新 更多