I believe that the only way to do this is to use On Error and handle the Subscript Out of Range error, which will be raised if the array (or the size of the array of interest to you) is not initialized.
eg.
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 generalize this to check how many dimensions exist in the 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 the comment:
It seems to me that a function cannot be easily generalized to work with any type of array
It can be easily generalized by specifying a Variant parameter and checking it as an array in the function body 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
source share