vba - 检查空数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10559804/
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
vba - checking for empty array
提问by TPR
Function IsVarArrayEmpty(anArray As Variant)
Dim i As Integer
On Error Resume Next
i = UBound(anArray, 1)
If Err.Number = 0 Then
IsVarArrayEmpty = False
Else
IsVarArrayEmpty = True
End If
End Function
It is returning true for uninitialized and false for initialized. I want to see if it has any data/content. However, the problem is I feel the above code is returning false even when there is no data in the array. How do I check that?
它为未初始化返回真,为初始化返回假。我想看看它是否有任何数据/内容。但是,问题是我觉得即使数组中没有数据,上面的代码也会返回 false。我如何检查?
(I tried setting string s equal to the byte array. That was "". That means the array is empty, right?)
(我尝试将 string s 设置为等于字节数组。那是“”。这意味着数组是空的,对吧?)
回答by assylias
I personally use this - now if you ReDim
an array with ReDim v (1 To 5) As Variant
, isArrayEmpty(v)
will return false because v
has 5 items, although they are all uninitialised.
我个人使用这个 - 现在如果你ReDim
的数组带有ReDim v (1 To 5) As Variant
,isArrayEmpty(v)
将返回 false 因为v
有 5 个项目,尽管它们都未初始化。
Public Function isArrayEmpty(parArray As Variant) As Boolean
'Returns true if:
' - parArray is not an array
' - parArray is a dynamic array that has not been initialised (ReDim)
' - parArray is a dynamic array has been erased (Erase)
If IsArray(parArray) = False Then isArrayEmpty = True
On Error Resume Next
If UBound(parArray) < LBound(parArray) Then
isArrayEmpty = True
Exit Function
Else
isArrayEmpty = False
End If
End Function
回答by sancho.s ReinstateMonicaCellio
I am simply pasting below the code by the great Chip Pearson.
Also check out his page on array functions.
我只是在伟大的 Chip Pearson 的代码下面粘贴。
还可以查看他关于数组函数的页面。
I hope this helps.
我希望这有帮助。
Public Function IsArrayEmpty(Arr As Variant) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IsArrayEmpty
' This function tests whether the array is empty (unallocated). Returns TRUE or FALSE.
'
' The VBA IsArray function indicates whether a variable is an array, but it does not
' distinguish between allocated and unallocated arrays. It will return TRUE for both
' allocated and unallocated arrays. This function tests whether the array has actually
' been allocated.
'
' This function is really the reverse of IsArrayAllocated.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim LB As Long
Dim UB As Long
err.Clear
On Error Resume Next
If IsArray(Arr) = False Then
' we weren't passed an array, return True
IsArrayEmpty = True
End If
' Attempt to get the UBound of the array. If the array is
' unallocated, an error will occur.
UB = UBound(Arr, 1)
If (err.Number <> 0) Then
IsArrayEmpty = True
Else
''''''''''''''''''''''''''''''''''''''''''
' On rare occassion, under circumstances I
' cannot reliably replictate, Err.Number
' will be 0 for an unallocated, empty array.
' On these occassions, LBound is 0 and
' UBound is -1.
' To accomodate the weird behavior, test to
' see if LB > UB. If so, the array is not
' allocated.
''''''''''''''''''''''''''''''''''''''''''
err.Clear
LB = LBound(Arr)
If LB > UB Then
IsArrayEmpty = True
Else
IsArrayEmpty = False
End If
End If
End Function
回答by Siddharth Rout
Try this
尝试这个
Sub Sample()
Dim Ar As Variant
Dim strTest As String
strg = "Blah"
Ar = Split(strg, "|")
Debug.Print "TEST1 : "; IsArrayEmpty(Ar)
strg = "Blah|Blah"
Ar = Split(strg, "|")
Debug.Print "TEST2 : "; IsArrayEmpty(Ar)
End Sub
Function IsArrayEmpty(Ar As Variant) As Boolean
If InStr(TypeName(Ar), "(") > 0 Then
If Not IsEmpty(Ar) Then
If UBound(Ar) > 0 Then
IsArrayEmpty = False
Else
IsArrayEmpty = True
End If
End If
End If
End Function
SNAPSHOT
快照
FOLLOWUP
跟进
does your code assume the array stores strings only? – TPG 7 mins ago
您的代码是否假定数组仅存储字符串?– TPG 7 分钟前
Yes. It did. If you want to test for all conditions then I would recommend using the API. Here is an example
是的。它做了。如果您想测试所有条件,那么我建议使用 API。这是一个例子
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Sub Sample()
Dim Ar() As Long
Debug.Print ArrayNotEmpty(Ar) '<~~ False
ReDim Ar(1)
Debug.Print ArrayNotEmpty(Ar) '<~~ True
End Sub
Public Function ArrayNotEmpty(Ar) As Boolean
Dim Ret As Long
CopyMemory Ret, ByVal VarPtr(Ar) + 8, ByVal 4
CopyMemory Ret, ByVal Ret, ByVal 4
ArrayNotEmpty = (Ret <> 0)
End Function