Count Array Dimensions

Function DimensionCount(aInput As Variant) As Long

    ‘Returns the number of dimensions of an array
    ‘aInput is an array
    
    Dim lDim As Long
    Dim lTemp As Long
    
    If IsArray(aInput) Then
        On Error Resume Next
            Do
                lDim = lDim + 1
                lTemp = LBound(aInput, lDim)
            Loop Until Err.Number <> 0
        On Error GoTo 0
        
        lDim = lDim - 1
    Else
        lDim = 0
    End If
    
    DimensionCount = lDim
    
End Function

3 Comments

  1. Rob van Gelder:

    Here’s the code I use for that purpose.

    Sub test()
    Dim arr(1, 2, 3, 4) As Long
    Dim i As Long

    On Error Resume Next
    Do: i = i - (LBound(arr, i + 1) * 0 = 0): Loop Until Err.Number
    On Error GoTo 0

    MsgBox i
    End Sub

  2. Colo:

    Very nice approach Rob!

  3. marco vismara:

    dl = UBound(WorksheetFunction.Transpose(divmatrix))

Leave a comment