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

Posted in Uncategorized

3 thoughts on “Count Array Dimensions

  1. 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


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.