Abstract

Wie ermittelt man die Anzahl der Dimensionen eines Arrays?

Appendix – ArrayDim Programmcode

Bitte den Haftungsausschluss im Impressum beachten.

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.com/arraydim_en/
'Source (DE): https://www.bplumhoff.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, 2, 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

Download

Bitte den Haftungsausschluss im Impressum beachten.

ArrayDim.xlsm [15 KB Excel Datei, ohne jegliche Gewährleistung]