vba inputBox:如何分辨“取消"与“取消"之间的区别和“确定"一个空的文本框 [英] vba inputBox: How to tell difference between "cancel" and "OK" with an empty textbox

查看:1055
本文介绍了vba inputBox:如何分辨“取消"与“取消"之间的区别和“确定"一个空的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下取消按钮时,vba inputBox返回一个空字符串.按下确定"后,它将返回其文本框中的任何文本. inputBox的第三个位置参数是其文本框中的初始文本.此参数的默认值为".

A vba inputBox returns an empty string when the cancel button is pressed. When OK is pressed, it returns whatever text is in its textbox. The inputBox's third positional argument is the initial text in its textbox. The default value for this argument is "".

在我的应用程序中,当他单击添加记录"按钮时,我使用inputBox要求用户指定新记录的名称.如果他按取消",那没问题:似乎他改变了对新唱片的想法.我退出潜艇.

In my application, I use an inputBox to ask a user to specify the name for a new record when he clicks an "Add Record" button. If he presses "cancel", no problem: seems he changed his mind about that new record. I exit the sub.

但是,如果他未能输入名称,或者输入名称并将其删除,我不想接受该名称,而是使用msgBox告诉他他必须指定唯一的记录名称.

However, if he fails to enter a name, or enters one and deletes it, I want to NOT accept that and instead use a msgBox to tell him he must specify a unique record name.

但是,似乎对于vba inputBox来说,用一个空的文本框来区分取消"和确定"之间的区别似乎不是直截了当的.我想知道如何做到这一点.

However, it seems that with vba inputBox it is not straight forward to tell the difference between "cancel" and "OK" with an empty textbox. I'd like to find out how to do that.

在寻找答案时,我发现了几个与此问题类似的问题,但是没有一个答案足以解决我的问题.

While looking for an answer, I found several questions similar to this one, but none had a complete enough answer to solve my problem.

推荐答案

有一种方法可以检查用户是否单击了取消"或只是输入了空字符串. 试试这个:

There is a way to check if user clicked "Cancel" or just entered empty string. Try this:

test = InputBox("Enter a value")
If StrPtr(test) = 0 Then
    MsgBox "Cancel"
ElseIf Len(test) = 0 Then
    MsgBox "No entry"
Else
    MsgBox "Correct"
End If

但是,这是一个非常粗略的解决方案.您可以在此处阅读有关StrPtr函数的更多信息: 有哪些好处和风险在VBA中使用StrPtr函数?

However it is quite sketchy solution. You can read more about StrPtr function here: What are the benefits and risks of using the StrPtr function in VBA?

这篇关于vba inputBox:如何分辨“取消"与“取消"之间的区别和“确定"一个空的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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