1

Is possible to create a function in Office VBA which can work with n-dimensional Array passed as parameter?

It means - the function receives some n-dimensional array (n is not known before). And then access each element in each dimensions. But how to do it when dimension count could be between 1 and many?

If I want to access the first element (consider Arrays starting from 1 here):

'1D
Array(1)
'2D
Array(1,1)
'...
'nD
Array(1,1, ... 1)

But I don't know how to create this dynamically

More real life example:

Dim MyArr() As String
ReDim MyArr(5, 2, 1)

'MyArr = "A", "B", "C", "D", "E"
'        "F", "G"
'        "H"

Dim Result As Long
Result = WorkWithArray(MyArr)

Function WorkWithArray(ByVal InputArray As Variant) As Long
    Dim ArrDimensions As Long
    ArrDimensions = DimCount(InputArray) 'Returns InputArr dimension count (here = 3)

    'How to access each element from an Array? 
    'Only solution what comes in my mind is this nasty case select monster:

        Select Case ArrDimensions 
        Case 1
            'Code for 1D Array e.g.: MyArr(1)
        Case 2
            'Code for 2D Array e.g.: MyArr(1,1)
'       ...
        'hundreds line of code later...
        Case 20
            'Code for 20D Array e.g.: MyArr(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
        'Mental asylum worker is knocking on my doors...
        Case Else
            'Code for too much dimensions
    End Select
End Function
Lluser
  • 246
  • 1
  • 10
  • 2
    The first duplicate is for how to get the number of dimensions. The second is for how to access an element at an index with variable number of dimensions. – GSerg Apr 21 '21 at 13:30
  • @GSerg Getting number of dimension is not part of the question. Thanks for the second link! – Lluser Apr 22 '21 at 06:12

0 Answers0