使用函数返回数组时出错

时间:2014-09-21 14:00:57

标签: arrays excel vba

我正在尝试使用一个函数为我的子程序返回一维数组,并且遇到了错误。下面是我正在使用的代码的简化版本:

Sub test()
Dim testArry(1 To 3) As Double

testArry = arryFunction(1.2) 'I get a compile error here "cant assign to array"
End Sub

Function arryFunction(cs As Double) As Double()
Dim anotherArry(1 To 3) As Double

anotherArry(1) = cs
anotherArry(2) = cs + 1
anotherArry(3) = cs + 3

arryFunction = anotherArry
End Function

我尝试过在线搜索,无法找到解决方案,至少有一个我理解的解决方案,会对此有所帮助。

1 个答案:

答案 0 :(得分:0)

虽然我还没有找到支持它的文档,但看起来你的错误是因为你无法将数组分配给另一个固定大小的数组。可能因为在复制期间阵列需要ReDim编辑,但这只是猜测。这确实有效

Sub test()
    Dim testArry() As Double
    testArry = arryFunction(1.2) 'I get a compile error here "cant assign to array"
End Sub