如何检测用户是否选择取消InputBox VBA Excel [复制]

时间:2021-07-19 23:52:01

This question already has an answer here:

这个问题在这里已有答案:

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

像str = vbCancel然后退出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) 如何检测用户是否选择取消InputBox VBA Excel [复制]

str = InputBox(提示:=“输入日期MM / DD / YYY”,_标题:=“日期确认”,默认值:=日期)

1 个解决方案

#1


23  

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类...

Your example

你的榜样

Private Sub test()
    Dim Str As String
    Str = InputBox("Enter Date MM/DD/YYY", "Date Confirmation", Now)
    If Str = vbNullString Then
        MsgBox ("User canceled!")
    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

#1


23  

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类...

Your example

你的榜样

Private Sub test()
    Dim Str As String
    Str = InputBox("Enter Date MM/DD/YYY", "Date Confirmation", Now)
    If Str = vbNullString Then
        MsgBox ("User canceled!")
    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