如何检测用户是否选择取消InputBox VBA Excel [英] How to detect if user select cancel InputBox VBA Excel

查看:675
本文介绍了如何检测用户是否选择取消InputBox VBA Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




如果str = vbCancel然后退出sub



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



str = InputBox(Prompt:=输入日期MM / DD / YYY,_
标题:=日期确认,默认值:=日期) / code>

解决方案

如果用户单击取消,返回零长度的字符串。您不能区分这一点,而不能输入一个空字符串。然而,您可以制作自己的自定义InputBox类...



您的示例

  Private Sub test()
Dim Str As String
Str = InputBox(输入日期MM / DD / YYY,Date Confirmation,现在)
如果Str = vbNullString Then
MsgBox(用户已取消!)
如果
End Sub

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



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


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

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)

解决方案

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

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

这篇关于如何检测用户是否选择取消InputBox VBA Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆