vba 检查 Variant 对象的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27396273/
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
Check length of Variant object
提问by user3748315
I want to find out if there are any elements in a list after applying filter to it. The list is of type variant which seems to be a problem. Most suggestions are in line with this post: but it does not work for me. What am i doing wrong?
我想在对其应用过滤器后找出列表中是否有任何元素。该列表是类型变体,这似乎是一个问题。大多数建议都与这篇文章一致:但它对我不起作用。我究竟做错了什么?
Dim message As Variant
Dim tempMessage As Variant
Dim a As Long
message = Split("AACP;CP;sBcp;ccffcp", ";")
tempMessage = Filter(message, "CP", False, vbTextCompare)
If IsNull(tempMessage) = True Then
Debug.Print "EMPTY"
Else
Debug.Print tempMessage(0)
End If
回答by Bathsheba
You have a couple of ways:
你有几种方法:
VarType(tempMessage)
will bevbEmpty
if it is completely blank.VarType(tempMessage) And vbArray
will be non-zero if it's an array type (even with zero elements. Zero length arrays areallowed in VBA). Here I'm usingAnd
in the logicalcontext.
VarType(tempMessage)
会vbEmpty
如果是完全空白的。VarType(tempMessage) And vbArray
如果它是数组类型,则为非零(即使元素为零。VBA中允许零长度数组)。这里我And
在逻辑上下文中使用。
If you've established it's an array, then you can take a guess at the dimensionality. (VBA provides no function for this, but luckily you should know from the documentation of Filter
).
如果你已经确定它是一个数组,那么你可以猜测维度。(VBA 没有为此提供任何功能,但幸运的是您应该从 的文档中知道Filter
)。
If you've established the variant is an array then use UBound(tempMessage) - LBound(tempMessage) + 1
to get the number of elements.
如果您已确定变体是一个数组,则使用它UBound(tempMessage) - LBound(tempMessage) + 1
来获取元素的数量。