freelensia
New Member
- Joined
- Jul 2, 2017
- Messages
- 18
I have this function to check if an array is empty. This code has worked before.
The input arr for this specific case is empty. Putting it in watch windows shows:
1. Expression: arr
2. Value: <blank>
3. Type: Variant/Variant()
4. Context: C_UnionTags.ArrayIsEmtpy
The code stops at UB = UBound(Arr, 1) without showing any errors. I have marked Break on all errors in Options - Settings - General tab.
Function ArrayIsEmpty(Arr As Variant) As Boolean
'VBA Arrays
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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
ArrayIsEmpty = 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
ArrayIsEmpty = 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
' UBoung 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
ArrayIsEmpty = True
Else
ArrayIsEmpty = False
End If
End If
End Function
The input arr for this specific case is empty. Putting it in watch windows shows:
1. Expression: arr
2. Value: <blank>
3. Type: Variant/Variant()
4. Context: C_UnionTags.ArrayIsEmtpy
The code stops at UB = UBound(Arr, 1) without showing any errors. I have marked Break on all errors in Options - Settings - General tab.