vba - checking for empty array
vba
Solution
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.
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
Problem
``` 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? (I tried setting string s equal to the byte array. That was "". That means the array is empty, right?)