I believe the only way to do this is to use On Error
and handle the Subscript Out of Range
error that will be raised if the array (or the dimension of the array you're interested in) isn't initialized.
E.g.
Public Function IsInitialized(arr() As String) As Boolean
On Error GoTo ErrHandler
Dim nUbound As Long
nUbound = UBound(arr)
IsInitialized = True
Exit Function
ErrHandler:
Exit Function
End Function
Dim a() As String
Dim b(0 To 10) As String
IsInitialized(a) ' returns False
IsInitialized(b) ' returns True
You can generalized this to test how many dimensions there are in an array, e.g.
Public Function HasAtLeastNDimensions(arr() As String, NoDimensions As Long) As Boolean
On Error GoTo ErrHandler
Dim nUbound As Long
nUbound = UBound(arr, NoDimensions)
HasAtLeastNDimensions = True
Exit Function
ErrHandler:
Exit Function
End Function
Dim a() As String
Dim b(0 To 10) As String
Dim c(0 To 10, 0 To 5) As String
HasAtLeastNDimensions(a, 1) ' False: a is not initialized
HasAtLeastNDimensions(b, 1) ' True: b has 1 dimension
HasAtLeastNDimensions(b, 2) ' False: b has only 1 dimension
HasAtLeastNDimensions(c, 2) ' True: c has 2 dimensions
UPDATE
In response to comment:
am i right in thinking that the function cannot be easily generalised to operate on any array type
It can be easily generalized by making the parameter a Variant, and checking it is an array in the body of the function using the IsArray
function:
Public Function HasAtLeastNDimensions(arr As Variant, NoDimensions As Long) As Boolean
On Error GoTo ErrHandler
Dim nUbound As Long
If Not IsArray(arr) Then Exit Function
nUbound = UBound(arr, NoDimensions)
HasAtLeastNDimensions = True
Exit Function
ErrHandler:
Exit Function
End Function