简体   繁体   English

Excel VBA传入2D数组的数组

[英]Excel VBA Passing in an array of 2D arrays

I am working with an array of 2D arrays in Excel VBA. 我正在使用Excel VBA中的2D数组数组。 I have a function... 我有功能...

Public Function constructStack(vbr() As Variant, hr As Integer) As stack

Where stack is a class I made. 我在哪里做的是堆栈。 I have another function, in which I am calling constructStack from. 我有另一个函数,我从中调用constructStack。 here is the call: 这里是电话:

Set stacks(i) = stack(i).constructStack(vbr(i), i)

vbr happens to be an array of 2D arrays. vbr恰好是2D阵列的阵列。 Seeing as vbr(i) would refer to a single 2D array of type variant, I'm confused why I'm getting the "Type mismatch: array or user-defined type expected," compile error. 看到vbr(i)指的是类型变量的单个2D数组,我感到困惑,为什么我会收到“类型不匹配:预期为数组或用户定义类型”的编译错误。

It's almost as if the compiler doesn't realize that vbr() will be filled with 24 2D arrays, which is why it's giving me the compile error. 几乎好像编译器没有意识到vbr()将被24个2D数组填充,这就是为什么它给了我编译错误。 Here is how I Dim vbr: 这是我如何暗淡vbr:

Dim vbr(1 To 24) As Variant

After declaring vbr, I eventually run this for loop which assigns each element of vbr a 2D array... 声明vbr之后,我最终运行此for循环,为vbr的每个元素分配一个2D数组...

vb = GetVBRSorted
For j = 1 To 24
    For i = 2 To 2000
        If (vb(i, 1)(j) <> "") Then
          lastFilleds(j) = i
        End If
    Next
Next
For j = 1 To 24
    ReDim vbrTemp(1 To lastFilleds(j) - 1, 1 To 5)
    For i = 2 To lastFilleds(j)
        For k = 1 To 5
            vbrTemp(i - 1, k) = vb(i, k)(j)
        Next
    Next
    vbr(j) = vbrTemp
Next

GetVBRSorted returns the exact same type as vbr - an array of 2D arrays. GetVBRSorted返回与vbr完全相同的类型-2D数组的数组。 If anyone has any input on this issue, it would be much appreciated. 如果有人在这个问题上有任何意见,将不胜感激。

the call you have .constructStack(vbr(i), i) is passing a single element of the array. 您拥有.constructStack(vbr(i), i)的调用正在传递数组的单个元素。 If you want to pass the whole array then you would use .constructStack(vbr, i) 如果要传递整个数组,则可以使用.constructStack(vbr, i)

Because each individual vbr(i) is a Variant , this is what you must declare as your parameter type. 由于每个vbr(i)都是Variant ,因此必须将其声明为参数类型。 There is no magical compile-time sniffing to realise the Variant contains an array. 没有神奇的编译时嗅探来实现Variant包含数组。

Use 采用

Public Function constructStack(vbr As Variant, hr As Integer) As stack 

Also see How can I use an optional array argument in a VBA procedure? 另请参见如何在VBA过程中使用可选的数组参数? .

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

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