How to return the number of dimensions of a (Variant) variable passed to it in VBA

user533978 picture user533978 · Aug 1, 2011 · Viewed 73.6k times · Source

Does anyone know how to return the number of dimensions of a (Variant) variable passed to it in VBA?

Answer

Jacob picture Jacob · Aug 1, 2011
Function getDimension(var As Variant) As Long
    On Error GoTo Err
    Dim i As Long
    Dim tmp As Long
    i = 0
    Do While True
        i = i + 1
        tmp = UBound(var, i)
    Loop
Err:
    getDimension = i - 1
End Function

That's the only way I could come up with. Not pretty….

Looking at MSDN, they basically did the same.