VBA 中使用 null 检查长度
我有一些代码旨在检查文本框中值的长度,如果任何框中没有内容,则字符串的长度为 0 (或 null)。代码如下:
If (Len(Form_MainScreen.Ctl48.Value) Or Len(Form_MainScreen.Ctl49.Value) Or _
Len(Form_MainScreen.Ctl50.Value) Or Len(Form_MainScreen.Ctl51.Value) Or _
Len(Form_MainScreen.Ctl52.Value) Or Len(Form_MainScreen.Ctl53.Value) Or _
Len(Form_MainScreen.Ctl54.Value) = 0) Then
Do X
Else
Do Y
End If
当一个字符串为空时,长度检查变为“null”,整个语句也变为“null”。 但如果长度检查全部不为空,则 if 语句变为“1”,然后再次执行 Do X 过程。
知道我能做什么吗?
谢谢, 蒂姆
I have some code that is meant to check the length of the values in the text boxes, and if any of the boxes has no content the length of the string is 0 (or null). Here is the code:
If (Len(Form_MainScreen.Ctl48.Value) Or Len(Form_MainScreen.Ctl49.Value) Or _
Len(Form_MainScreen.Ctl50.Value) Or Len(Form_MainScreen.Ctl51.Value) Or _
Len(Form_MainScreen.Ctl52.Value) Or Len(Form_MainScreen.Ctl53.Value) Or _
Len(Form_MainScreen.Ctl54.Value) = 0) Then
Do X
Else
Do Y
End If
When one string is blank, the length check becomes "null" and so does the whole statement.
But if the length checks are all not null, the if statement becomes a "1" and then procedes to execute the Do X procedure again.
Any idea of what I can do?
Thanks,
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码片段对我来说没有多大意义,但如果您使用 vb.net 并且您希望“X”在任何值为 null 或 0 长度时执行,那么您可以执行以下操作:
使用
String.IsNullOrEmpty
是一个内置函数,它将无一例外地处理这两种情况。使用OrElse
条件将允许稍微更快的处理速度,因为一旦收到有效的匹配,它将停止执行条件表达式。简单地使用Or
需要评估所有条件表达式,即使这不是必需的。Your code snippet doesn't make much sense to me, but if you're using vb.net and you want "X" to execute if ANY of the values is null or 0 length then you could do the following:
Using
String.IsNullOrEmpty
is a built-in function that will handle both conditions without exception. Using theOrElse
conditional will allow slightly faster processing in that it will stop executing the conditional expressions once it receives a valid match. Using simplyOr
requires all conditional expressions to be evaluated even if it's not necessary.