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

编程入门 行业动态 更新时间:2024-10-19 21:25:55
本文介绍了如何检测用户是否选择取消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

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

请参阅 msdn.microsoft/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 msdn.microsoft/en-us/library/6z0ak68w(v=vs.90).aspx

更多推荐

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

本文发布于:2023-11-27 09:14:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637457.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   InputBox   Excel   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!