vba-检查空数组
对于未初始化,返回true;对于已初始化,返回false。我想看看它是否有任何数据/内容。然而,问题是我觉得上面的代码返回false,即使数组中没有数据。我怎么检查 (我尝试将字符串s设置为等于字节数组。即“”。这意味着数组为空,对吗?试试这个vba-检查空数组,vba,Vba,对于未初始化,返回true;对于已初始化,返回false。我想看看它是否有任何数据/内容。然而,问题是我觉得上面的代码返回false,即使数组中没有数据。我怎么检查 (我尝试将字符串s设置为等于字节数组。即“”。这意味着数组为空,对吗?试试这个 Function IsVarArrayEmpty(anArray As Variant) Dim i As Integer On Error Resume Next i = UBound(anArray, 1) If Err.Number =
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
快照
跟进
您的代码是否假定数组仅存储字符串TPG 7分钟前 对。是的。如果您想测试所有条件,那么我建议使用API。这里有一个例子
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
Private声明子CopyMemory Lib“kernel32”别名“rtlmovemory”_
(pDst为任意项,pSrc为任意项,ByVal ByteLen为任意项)
子样本()
Dim Ar()的长度为
Debug.Print ArrayNotEmpty(Ar)我个人使用它-现在如果你ReDim
将ReDim v(1到5)作为变量的数组,isArrayEmpty(v)
将返回false,因为v
有5项,尽管它们都未初始化
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
公共函数isArrayEmpty(parArray作为变量)作为布尔值
'在以下情况下返回true:
'-parArray不是数组
“-parArray是尚未初始化的动态数组(ReDim)
'-parArray是已擦除的动态数组(擦除)
如果IsArray(parArray)=False,则isArrayEmpty=True
出错时继续下一步
如果UBound(parArray)
我只是在伟大的Chip Pearson的代码下面粘贴。
也看看他的
我希望这有帮助
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
公共函数IsArrayEmpty(Arr作为变量)作为布尔值
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
“我饿了
'此函数测试数组是否为空(未分配)。返回TRUE或FALSE。
'
'VBA IsArray函数指示变量是否为数组,但它不是
'区分已分配数组和未分配数组。这两种情况都会恢复为真
'已分配和未分配的数组。此函数用于测试数组是否实际
“已经分配了。
'
'此功能实际上与IsArrayalLocation相反。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
暗磅一样长
长得一样
呃,明白了
出错时继续下一步
如果IsArray(Arr)=False,则
'我们没有通过数组,返回True
IsArrayEmpty=True
如果结束
'尝试获取数组的UBound。如果数组是
'未分配,将发生错误。
UB=UBound(Arr,1)
如果(错误编号0),则
IsArrayEmpty=True
其他的
''''''''''''''''''''''''''''''''''''''''''
“在罕见的情况下,我
'无法可靠地答复,错误号
'对于未分配的空数组将为0。
'在这些情况下,LBound是0和
“UBound是-1。
“为了适应这种奇怪的行为,测试
'看看LB>UB。如果是,则数组不可用
“分配。
''''''''''''''''''''''''''''''''''''''''''
呃,明白了
LB=LBound(Arr)
如果LB>UB那么
IsArrayEmpty=True
其他的
IsArrayEmpty=False
如果结束
如果结束
端函数
您的代码是否假定数组只存储字符串?+1我已经使用这个函数以及他的一些其他数组助手函数一段时间了。您发布了。
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