I am writing a script that will loop through an Excel spreadsheet and find if there are duplicates of selected cells. If there are duplicates then the function will return
Chip Pearson gave the answer years ago and it still works. Here's the function I've had in my library for almost four years.
Public Function IsArrayEmpty(arr As Variant) As Boolean
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 occasion, under circumstances I
' cannot reliably replicate, Err.Number
' will be 0 for an unallocated, empty array.
' On these occasions, LBound is 0 and
' UBound is -1.
' To accommodate 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
Err.Clear
End Function
Basically, it checks to make sure you passed an array, then it attempts to find the upper bound (which will throw an error if the array is empty), and finally it compares the lower bound to the upper bound to make sure the array really isn't empty.