简体   繁体   English

ReDim中的VBA参数列表和参数列表作为数组的索引

[英]VBA Param List in ReDim and Param List as index of array

I'm building a module with the purpose to ease the 'Redim' of a multidimentionnal array. 我正在构建一个模块,以简化多维数组的“ Redim”。

The main function is IncDimension which take an array, the dimension you want to redefine and the number of place to add or remove. 主要功能是IncDimension,它采用数组,您要重新定义的维度以及要添加或删除的位置的数量。

Public sub main()
 Dim v as variant

 ReDim a (1 to 3, 1 to 4, 1 to 5)
 Call IncDimension(a, 1, 5)
 'Before a (1 to 3, 1 to 4, 1 to 5)
 'After  a (1 to 8, 1 to 4, 1 to 5)
End sub

It works fine when the number of dimension is between 1 and 9 because I made sure every case were implemented. 当维数在1到9之间时,它可以正常工作,因为我确保每种情况都已实现。 BUT let's say I might need to work with an array with more than 9 dimensions, my module is useless. 但是,我可能需要处理9个以上维度的数组,但是我的模块没用。

There are 2 places where I had to use a Select Case based on the number of dimension : When I want to 'Redim' the array 在2个地方,我不得不根据维数使用Select Case:当我想“重设”数组时

    Select Case nbDimension
    Case 1
        ReDim Preserve arr( _
        LBound(arr, 1) To UBound(arr, 1) + i)
    Case 2
        ReDim Preserve arr( _
        LBound(arr, 1) To UBound(arr, 1), _
        LBound(arr, 2) To UBound(arr, 2) + i)
    Case 3
        ReDim Preserve arr( _
        LBound(arr, 1) To UBound(arr, 1), _
        LBound(arr, 2) To UBound(arr, 2), _
        LBound(arr, 3) To UBound(arr, 3) + i)
'etc.

When I want to move value in the temp array (tbl) 当我想在临时数组(tbl)中移动值时

    Select Case nbDimension
    Case 2
        tbl(lstTransferts(1), lstTransferts(2)) = arr(lstIndex(1), lstIndex(2))
    Case 3
        tbl(lstTransferts(1), lstTransferts(2), lstTransferts(3)) = arr(lstIndex(1), lstIndex(2), lstIndex(3))
'etc.

So, is there a way to use a list of value in ReDim Preserve and is there a way to use a list of index when accessing an array . 因此, 有没有办法在ReDim Preserve中使用值列表,并且有办法在访问数组时使用索引列表

You should stop this... This doesn't make sense. 您应该停止此操作……这没有意义。 In first place, why do you need such a multi dimensional array? 首先,为什么需要这样的多维数组? I never used more than 2 dimensions, and I didn't find any use for larger variants. 我从来没有使用超过2个维度,也没有发现任何较大的变体。 This clearly seems like a request for a bad solution to an ill-posed problem. 显然,这似乎是要求为解决不适的问题提供不好的解决方案。 What's your real problem? 你真正的问题是什么?

I dynamically created a module based on the number of dimensions. 我根据维数动态创建了一个模块。

Private Sub creerModuleSelonDimension(ByRef tbl As Variant, ByRef arr As Variant, ByRef lstDimensions As Variant)
Dim vbComp As Object
Dim useReDim As String
Dim dynamicReDim As String
Dim app As Object
Dim i As Integer

'Create the ReDim statement dynamically
dynamicReDim = "ReDim tbl( _" & vbNewLine
For i = LBound(lstDimensions) To UBound(lstDimensions)
    dynamicReDim = dynamicReDim + "LBound(arr, lstDimensions(" & i & ")) to UBound(arr, lstDimensions(" & i & ")), _ " & vbNewLine
Next i
dynamicReDim = Left(dynamicReDim, Len(dynamicReDim) - 6) & " _ " & vbNewLine & ")"

'Create the sub
useReDim = "Public Sub useReDim(ByRef tbl as Variant, ByRef arr as Variant, ByRef lstDimensions as variant)" & vbNewLine & dynamicReDim & vbNewLine & "End Sub"

'Create a new module and add the sub in it
Set vbComp = ThisWorkbook.VBProject.VBComponents.Add(1)
vbComp.CodeModule.AddFromString useReDim

'Allow the use of ByRef variables in Application.Run
Set app = Application

'Run useReDim with ByRef variables
i = app.Run(vbComp.Name & ".useReDim", tbl, arr, lstDimensions)

'Delete the new module
ThisWorkbook.VBProject.VBComponents.Remove vbComp
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM