【问题标题】:How to detect if user select cancel InputBox VBA Excel如何检测用户是否选择取消 InputBox VBA Excel
【发布时间】:2014-10-08 19:23:23
【问题描述】:

我有一个输入框,要求用户输入日期。如果用户单击取消或关闭输入对话框而不是按确定,我如何让程序知道停止。

类似的东西 if str=vbCancel then exit sub

目前,用户可以点击确定或取消,但程序仍在运行

str = InputBox(Prompt:="Enter Date MM/DD/YYY", _ Title:="Date Confirmation", Default:=Date)

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    如果用户单击取消,则返回零长度字符串。您无法将此与输入空字符串区分开来。但是,您可以制作自己的自定义 InputBox 类...

    EDIT 根据this answer 正确区分空字符串和取消。

    你的例子

    Private Sub test()
        Dim result As String
        result = InputBox("Enter Date MM/DD/YYY", "Date Confirmation", Now)
        If StrPtr(result) = 0 Then
            MsgBox ("User canceled!")
        ElseIf result = vbNullString Then
            MsgBox ("User didn't enter anything!")
        Else
            MsgBox ("User entered " & result)
        End If
    End Sub
    

    当他们删除默认字符串时会告诉用户他们取消了,或者他们点击取消。

    http://msdn.microsoft.com/en-us/library/6z0ak68w(v=vs.90).aspx

    【讨论】:

    • 谢谢 - 它应该是 Vbnullstring 而不是 vbCancel
    • @DanVerdolino 如果您使用声明的 String 变量进行测试,那就是真的。但正如 Siddharth 指出的那样,您也可以使用按下的按钮。
    【解决方案2】:

    以下示例使用 InputBox 方法验证用户输入以取消隐藏工作表: 这里重要的是在 StrPtr 中使用 wrap InputBox 变量,这样当用户选择单击 InputBox 上的“x”图标时,它可以与“0”进行比较。

    Sub unhidesheet()
    
    Dim ws As Worksheet
    Dim pw As String
    
    pw = InputBox("Enter Password to Unhide Sheets:", "Unhide Data Sheets")
    If StrPtr(pw) = 0 Then
    
       Exit Sub
    ElseIf pw = NullString Then
       Exit Sub
    ElseIf pw = 123456 Then
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next
    End If
    End Sub
    

    【讨论】:

      【解决方案3】:

      上述解决方案不适用于所有 InputBox-Cancel 情况。最值得注意的是,如果您必须 InputBox 一个 Range,它确实不起作用

      例如,尝试使用以下 InputBox 定义自定义范围('sRange',类型:=8,需要 Set + Application.InputBox),然后按取消时会出现错误:

      Sub Cancel_Handler_WRONG()
      Set sRange = Application.InputBox("Input custom range", _
          "Cancel-press test", Selection.Address, Type:=8)
      If StrPtr(sRange) = 0 Then  'I also tried with sRange.address and vbNullString
          MsgBox ("Cancel pressed!")
          Exit Sub
      End If
          MsgBox ("Your custom range is " & sRange.Address)
      End Sub
      

      在这种情况下,唯一有效的是“On Error GoTo ErrorHandler”语句最后的 InputBox + ErrorHandler 之前:

      Sub Cancel_Handler_OK()
      On Error GoTo ErrorHandler
      Set sRange = Application.InputBox("Input custom range", _
          "Cancel-press test", Selection.Address, Type:=8)
      MsgBox ("Your custom range is " & sRange.Address)
      Exit Sub
      ErrorHandler:
          MsgBox ("Cancel pressed")
      End Sub
      

      那么,问题是如何使用 If 语句检测或者错误 StrPtr()=0?

      【讨论】:

      • StrPtr()=0 如果您使用Application.InputBox,检查将不起作用。为此,您只需使用InputBox
      • 此解决方案将输入框方法Application.Inputbox 与函数Inputbox 混淆。如果用户按下取消,则该方法返回False,因此给出的答案不正确。
      【解决方案4】:

      如果你的输入框是一个数组,那就不行了。我已经通过首先检查它是否是一个数组来解决它。

      Dim MyArrayCheck As String
      Dim MyPlateMapArray as variant
      MyPlateMapArray = Application.InputBox("Select ....", Type:=8)
      
      MyArrayCheck = IsArray(MyPlateMapArray)
      If MyArrayCheck = "False" Then
      Exit Sub
      End If
      

      【讨论】:

        【解决方案5】:

        另一个建议。

        inputbox 返回空值时创建一个消息框。示例:

        Dim PrC as string = MsgBox( _
        "No data provided, do you want to cancel?", vbYesNo+vbQuestion, "Cancel?")
        

        【讨论】:

          【解决方案6】:

          Sub TestInputBox() 将文本暗淡为字符串 text = InputBox("输入一些文字") 如果文本 = "" 那么 MsgBox "按钮取消被按下或没有输入" 别的 消息框文本 万一 结束子

          【讨论】:

            猜你喜欢
            • 2022-01-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-11-05
            • 1970-01-01
            • 2010-12-08
            相关资源
            最近更新 更多