vba 如果文本框为空,如何防止代码执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12369152/
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
How to prevent the code execution if a textbox is empty?
提问by Alegro
I want to prevent the code execution if user leave a textbox empty
如果用户将文本框留空,我想阻止代码执行
If tx01.Value = "" Then Exit Sub
If tx01.Value = vbNullString Then Exit Sub // also tried
But user can simply write a space (or any number of spaces) and - prevention doesn't work.
I need characters in this box (numeric and nonNumeric), but not only spaces.
How can I do this, pls ?
但是用户可以简单地写一个空格(或任意数量的空格)并且 - 预防不起作用。
我需要这个框中的字符(数字和非数字),但不仅仅是空格。
我该怎么做,请?
回答by A Developer
Try adding a Trim()
in the textbox value check.
尝试Trim()
在文本框值检查中添加一个。
回答by Tony Hopkinson
something like
就像是
if trim(tx01.Value) = "" then Exit sub
Trim removes leading and trailing whitespace characters.
Trim 删除前导和尾随空白字符。