Originally Posted by
xlnitwit
Will you ever have more than 3 dimensions?
Yes I would like to have that support and support for freely swaping, deleting (row, columns,elements...), combining, reshaping arrays.
Two ways for dynamical creation of n-dimensional array
1. Using VBA Project object model
2. Very long ifs structure like bellow:
Function narr(str As String)
'''''''''''''''''''''''''''''''''''''''
' Declare n-dimensional array
'''''''''''''''''''''''''''''''''''''''
Dim barr() As String
Dim rarr() As Variant 'resulting array
Dim nd As Long
barr = Split(str, ",")
nd = size(barr) 'number of elements in splited array (perhaps there is native function which counts all elements of an array)
If nd = 0 Then
narr = rarr
End If
If nd = 1 Then
ReDim rarr(barr(0))
ElseIf nd = 2 Then
ReDim rarr(barr(0), barr(1))
ElseIf nd = 3 Then
ReDim rarr(barr(0), barr(1), barr(2))
End If
narr = rarr
End Function
Sub tnarr()
'''''''''''''''''''''''''''''''''''''''
' test function narr
'''''''''''''''''''''''''''''''''''''''
Dim sr As String
Dim rar() As Variant
sr = "10,10,20"
rar = narr(sr)
End Sub
Function size(arr As Variant)
'''''''''''''''''''''''''''''''''''''
' Get number of elements in array
'''''''''''''''''''''''''''''''''''''
' using bounds may (should) be faster
Dim cl As Variant
Dim i As Long
For Each cl In arr
i = i + 1
Next cl
size = i
End Function
I found some ways how to implement array manipulation, none of which is pretty.
1. Using loops for copying one by one to another array (usable but slow)
2. Using sheet ranges (extremely slow)
3. Using Join and Split, for each dimension different separator (works only on String arrays, probably slow too)
4. Using nested arrays like Array(Array(1, 1, 1), Array(1, 1, 1)).
This is probably dead end, since Application.Evaluate("Array(Array(1, 1, 1), Array(1, 1, 1))(1)") yields Error 2029
5. Perhaps user defined Class?
6. Using external dll VarPtr http://stackoverflow.com/questions/7...tain-value-vba
Option 6. Is interesting, though there may be problem on systems where particular dll is missing and with restricted user rights. That is big drawback. Perhaps I can reference dll outside system folders or bundle all my modules and dlls in one file?
Bookmarks