Is there a way to find out how many dimensions a given array has? And the total ammount of elements in the array? Please help me. :)
Printable View
Is there a way to find out how many dimensions a given array has? And the total ammount of elements in the array? Please help me. :)
You can do it via API :)
This can tell you the number of dimensions:
You can get the number of items in this way:Code:
Declare Function ArrayPtr Lib "msvbvm60" Alias "VarPtr" (ptr() As Any) As Long
Declare Function CopyMemoryA Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal BytesLen As Long) As Long
'Use like this: ArrayDimensionCount(ArrayPtr(p()))
Function admArrayDimensionCount(lpA As Long) As Long
' Test: passed
Dim Dims As Integer
Dim lpA2 As Long
Call CopyMemoryA(lpA2, ByVal lpA, 4)
If (lpA2 > 0) Then
Call CopyMemoryA(Dims, ByVal lpA2, 2)
admArrayDimensionCount = Dims
Else
admArrayDimensionCount = 0
End If
End Function
CyaCode:Count = UBound(myArray) - LBound(myArray) + 1
The easy way...
Public Function ArrayDimensions(ParamArray Arr() As Variant) As Integer
Dim K As Integer, DM As Integer
On Error GoTo Err_Exit
For K = 1 To 1000
DM = UBound(Arr(0), K)
Next K
ArrayDimensions = -1
Exit Function
Err_Exit:
ArrayDimensions = K - 1
Err.Clear
End Function
Forgot to put the code tangs :D
Code:Public Function ArrayDimensions(ParamArray Arr() As Variant) As Integer
Dim K As Integer, DM As Integer
On Error GoTo Err_Exit
For K = 1 To 1000
DM = UBound(Arr(0), K)
Next K
ArrayDimensions = -1
Exit Function
Err_Exit:
ArrayDimensions = K - 1
Err.Clear
End Function