Abstract
How do you determine the number of dimensions of an array?
Appendix – ArrayDim Code
Please read my Disclaimer.
Option Explicit
Function ArrayDim(v As Variant) As Long
'Returns number of dimensions of an array or 0 for
'an undimensioned array or -1 if no array at all.
'Source (EN): https://www.sulprobil.de/arraydim_en/
'Source (DE): https://www.berndplumhoff.de/arraydim_de/
'(C) (P) by Bernd Plumhoff 10-May-2010 PB V0.1
Dim i As Long
ArrayDim = -1
If Not IsArray(v) Then Exit Function
On Error Resume Next
'Err.Clear 'Not necessary
Do While IsNumeric(UBound(v, i + 1))
If Err.Number <> 0 Then Exit Do
i = i + 1
Loop
ArrayDim = i
End Function
Sub test()
'Expected output: -1, 0, 1, 3
Dim v
Dim w()
Dim x(1 To 2)
Dim y(1, 2, 3)
Debug.Print ArrayDim(v)
Debug.Print ArrayDim(w)
Debug.Print ArrayDim(x)
Debug.Print ArrayDim(y)
End Sub
Please read my Disclaimer.
ArrayDim.xlsm [16 KB Excel file, open and use at your own risk]