简体   繁体   中英

Excel VBA Passing in an array of 2D arrays

I am working with an array of 2D arrays in Excel VBA. 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. here is the call:

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

vbr happens to be an array of 2D arrays. 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.

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. Here is how I Dim 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...

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. 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. If you want to pass the whole array then you would use .constructStack(vbr, i)

Because each individual vbr(i) is a Variant , this is what you must declare as your parameter type. There is no magical compile-time sniffing to realise the Variant contains an array.

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? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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