Access VBA在函数中传递多维数组 [英] Access VBA Pass a MultiDimensional Array in a function
问题描述
Access 2013 32位:Win 7 64位
Access 2013 32 bit: Win 7 64bit
尝试:是否可以将多维数组作为EXCEL VBA中的参数传递?无济于事
(如果您可以回答这个问题,您可能可以回答,但是有些不同)
(If you can answer this you can probably answer that, but they're a little different)
Sub CreateArray()
Dim myArray(1 to 10, 1 to 5)
'Code that assigns values to (1 to 10, 1 to 4)
myArray() = CalculateLastColofArray(myArray())
'Do stuff with full array
End sub
Function CalculateLastColofArray(calcArray)
For i = LBound(calcArray()) to UBound(calcArray())
calcArray(i,5) = calcArray(i,1) + calcArray(i,3)
Next i
CalculateLastColofArray = calcArray()
End Function
我的计算实际上比简单的加法复杂得多,数组是动态大的(x,5)
My calculation is actually much more complex than the simple addition and my array is dynamically large (x, 5)
按照上面显示的方式填充myArray,但是调试时显示为将鼠标悬停在函数中时进入函数之前,调用它并报错。
Doing it the way I have shown above fills myArray, but debugging has it shown as when I hover over it wrapped in the function call and it errors before entering the function
推荐答案
您可以将多维数组作为参数传递。
You can pass multidimensional array as a parameter.
您也可以将函数的结果分配给数组变量,但是必须将此变量声明为动态数组(因此不能在声明行中指定其尺寸)。
You can also assign the result of the function to the array variable, however this variable must be declared as dynamic array (so its dimensions cannot be specified in declaring line).
此外,您的代码中还有其他错误。下面是正确的版本:
Furthermore, you have some other errors in your code. Below is the correct version:
Sub CreateArray()
'Dim myArray(1 To 10, 1 To 5) As Variant
Dim myArray() As Variant
ReDim myArray(1 To 10, 1 To 5)
'Code that assigns values to (1 to 10, 1 to 4)
myArray = CalculateLastColofArray(myArray)
'Do stuff with full array
End Sub
Function CalculateLastColofArray(calcArray() As Variant) As Variant()
For i = LBound(calcArray) To UBound(calcArray)
calcArray(i, 5) = calcArray(i, 1) + calcArray(i, 3)
Next i
CalculateLastColofArray = calcArray
End Function
这篇关于Access VBA在函数中传递多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!