如何检测用户是否选择取消 InputBox VBA Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26264814/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:00:56  来源:igfitidea点击:

How to detect if user select cancel InputBox VBA Excel

excelvba

提问by user2103670

I have an input box asking user to enter a date. How do I let the program know to stop if the user click cancel or close the input dialog instead of press okay.

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

Something like if str=vbCancel then exit sub

就像是 if str=vbCancel then exit sub

Currently, user can hit OK or Cancel but the program still runs

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

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

str = InputBox(Prompt:="Enter Date MM/DD/YYY", _ Title:="Date Confirmation", Default:=Date)在此处输入图片说明

回答by djv

If the user clicks Cancel, a zero-length string is returned. You can't differentiate this from entering an empty string. You can however make your own custom InputBox class...

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

EDITto properly differentiate between empty string and cancel, according to this answer.

根据这个答案编辑以正确区分空字符串和取消。

Your example

你的榜样

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

Would tell the user they canceled when they delete the default string, or they click cancel.

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

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

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

回答by Vasko

The solution above does not work in all InputBox-Cancel cases.

上述解决方案不适用于所有 InputBox-Cancel 情况。

For example, try the following InputBox for defining a custom range ('sRange', type:=8, requires set + application.inputbox) and you will get an error upon pressing Cancel:

例如,尝试使用以下 InputBox 来定义自定义范围 ('sRange', type:=8, requires set + application.inputbox) 并且在按 Cancel 时会出现错误:

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

The only thing that works, in this case, is an "On Error GoTo ErrorHandler" statement beforethe InputBox + ErrorHandler at the end:

在这种情况下,唯一有效的是最后 InputBox + ErrorHandler之前的“On Error GoTo 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

So, the question is how to detect eitheran error orStrPtr()=0 with an Ifstatement?

所以,问题是如何检测任何错误与StrPtr()= 0,如果语句?