为什么我不能将数组(在数组中)作为参数传递?

时间:2022-11-01 21:48:54

I have an array of arrays arrAggregatedArrays(1 to 8)

我有一个数组数组arrAggregatedArrays(1到8)

I can call a sub like this:

我可以像这样调用一个子:

call sub(ArrNewClient)

But I get a compile error: "Type Mismatch" if I try this:

但我得到一个编译错误:“类型不匹配”,如果我尝试这样:

call sub(arrAggregatedArrays(1))

Why? And is there a way around it?

为什么?它有办法吗?

and why does it not recognise arrAggregatedArrays(1) as an array even though it will perform functions like UBound on it like normal?

为什么它不能将arrAggregatedArrays(1)识别为数组,即使它像普通一样执行像UBound这样的函数?

Public arrAggregatedArrays()     As Variant      '/ Holds all the sheet-Data Arrays  

'/ Declared in a seperate module

'/在一个单独的模块中声明

      ReDim arrAggregatedArrays(1 To 8)
            arrAggregatedArrays(1) = arrNewClient
            arrAggregatedArrays(2) = arrExistingClient
            arrAggregatedArrays(3) = arrGroupSchemes
            arrAggregatedArrays(4) = arrOther
            arrAggregatedArrays(5) = arrMcOngoing
            arrAggregatedArrays(6) = arrJhOngoing
            arrAggregatedArrays(7) = arrAegonQuilterArc
            arrAggregatedArrays(8) = arrAscentric

      Call FilterSheetArrayForColumns(arrAggregatedArrays(1))

Public Sub FilterSheetArrayForColumns(ByRef arrCurrentArray() As Variant)

and a screenshot:

和截图:

为什么我不能将数组(在数组中)作为参数传递?

2 个解决方案

#1


6  

You can create a Variant array in one of two ways:

您可以使用以下两种方法之一创建Variant数组:

Dim v1() As Variant
Dim v2: v2 = Array()

With the former, you receive the array as a subroutine parameter using the v1() notation, like with any other data type array in VBA. With the latter, you'll need to receive it as a normal variable, without the array notation.

对于前者,使用v1()表示法将数组作为子例程参数接收,与VBA中的任何其他数据类型数组一样。对于后者,您需要将其作为普通变量接收,而不使用数组表示法。

Variants are special because they can hold many types, including array types, which is why the v = Array() syntax works. When done this way, they should be treated like any other variable and passed that way in and out of subroutines.

变体是特殊的,因为它们可以包含许多类型,包括数组类型,这就是v = Array()语法的工作原理。当这样做时,它们应该被视为任何其他变量,并以这种方式进出子程序。

#2


1  

As mentioned in comments, you need to show more on the implementation you are using. This works for me.

如评论中所述,您需要显示有关您正在使用的实现的更多信息。这对我有用。

Sub arr_test()
    Dim arr As Variant

    arr = Array(Array(1, 2, 3), Array(2, 3, 4), _
                Array(3, 4, 5), Array(4, 5, 6))

    Debug.Print LBound(arr, 1) & ":" & UBound(arr, 1)
    Debug.Print LBound(arr(1), 1) & ":" & UBound(arr(1), 1)

    Call arr_sub(arr)
    Call arr_sub(arr(1))

End Sub

Sub arr_sub(tarr As Variant)
    Debug.Print "arr_sub - "; LBound(tarr, 1) & ":" & UBound(tarr, 1)
End Sub

Results from the Immediate window:

立即窗口的结果:

arr_test
0:3
0:2
arr_sub - 0:3
arr_sub - 0:2

#1


6  

You can create a Variant array in one of two ways:

您可以使用以下两种方法之一创建Variant数组:

Dim v1() As Variant
Dim v2: v2 = Array()

With the former, you receive the array as a subroutine parameter using the v1() notation, like with any other data type array in VBA. With the latter, you'll need to receive it as a normal variable, without the array notation.

对于前者,使用v1()表示法将数组作为子例程参数接收,与VBA中的任何其他数据类型数组一样。对于后者,您需要将其作为普通变量接收,而不使用数组表示法。

Variants are special because they can hold many types, including array types, which is why the v = Array() syntax works. When done this way, they should be treated like any other variable and passed that way in and out of subroutines.

变体是特殊的,因为它们可以包含许多类型,包括数组类型,这就是v = Array()语法的工作原理。当这样做时,它们应该被视为任何其他变量,并以这种方式进出子程序。

#2


1  

As mentioned in comments, you need to show more on the implementation you are using. This works for me.

如评论中所述,您需要显示有关您正在使用的实现的更多信息。这对我有用。

Sub arr_test()
    Dim arr As Variant

    arr = Array(Array(1, 2, 3), Array(2, 3, 4), _
                Array(3, 4, 5), Array(4, 5, 6))

    Debug.Print LBound(arr, 1) & ":" & UBound(arr, 1)
    Debug.Print LBound(arr(1), 1) & ":" & UBound(arr(1), 1)

    Call arr_sub(arr)
    Call arr_sub(arr(1))

End Sub

Sub arr_sub(tarr As Variant)
    Debug.Print "arr_sub - "; LBound(tarr, 1) & ":" & UBound(tarr, 1)
End Sub

Results from the Immediate window:

立即窗口的结果:

arr_test
0:3
0:2
arr_sub - 0:3
arr_sub - 0:2