EXCEL VBA 检查条目是否为空或不是“空格”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14108948/
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
EXCEL VBA Check if entry is empty or not 'space'
提问by 4 Leave Cover
Note.Check if the TextBox1
is empty is easy by using TextBox1.Value = ""
.
笔记。TextBox1
使用TextBox1.Value = ""
.检查是否为空很容易。
But the problem is when the user hit the spacebar
, TextBox1
will still recognize it as a value. In such case, my data will appear as an empty cell with 1 space
inside. So my questionis, is there any method to check TextBox1.value
for empty and also not consist of space
whether there are 1 or more space
? Million thanks to all.
但问题是当用户点击 时spacebar
,TextBox1
仍会将其识别为一个值。在这种情况下,我的数据将显示为space
内部为 1 的空单元格。所以我的问题是,是否有任何方法可以检查是否TextBox1.value
为空并且不包括space
是否有 1 个或多个space
?万感谢大家。
回答by Lord Peter
A common trick is to check like this:
一个常见的技巧是像这样检查:
trim(TextBox1.Value & vbnullstring) = vbnullstring
this will work for spaces, empty strings, and genuine null values
这适用于空格、空字符串和真正的空值
回答by pyrospade
Most terse version I can think of
我能想到的最简洁的版本
Len(Trim(TextBox1.Value)) = 0
If you need to do this multiple times, wrap it in a function
如果您需要多次执行此操作,请将其包装在一个函数中
Public Function HasContent(text_box as Object) as Boolean
HasContent = (Len(Trim(text_box.Value)) > 0)
End Function
Usage
用法
If HasContent(TextBox1) Then
' ...
回答by Kanwaljeet Mehta
Here is the code to check whether value is present or not.
这是检查值是否存在的代码。
If Trim(textbox1.text) <> "" Then
'Your code goes here
Else
'Nothing
End If
I think this will help.
我认为这会有所帮助。
回答by Ashley Niekerk
You can use the following code to check if a textbox object is null/empty
您可以使用以下代码检查文本框对象是否为空/空
'Checks if the box is null
If Me.TextBox & "" <> "" Then
'Enter Code here...
End if