Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 函数未正确返回数组,尽管可以在Debug.Print中看到_Arrays_Excel_Vba_Excel Formula - Fatal编程技术网

Arrays 函数未正确返回数组,尽管可以在Debug.Print中看到

Arrays 函数未正确返回数组,尽管可以在Debug.Print中看到,arrays,excel,vba,excel-formula,Arrays,Excel,Vba,Excel Formula,我正在编写一个Excel VBA函数,该函数返回一个数组以在其他函数中使用。当我在Excel工作表中测试GenerateBlendedReturnSeries函数并使用ctlr shift enter查看完整结果时,整个数组都是零。然而,奇怪的是,当我检查Debug.Print BlendedReturnSeriesArray300,1时,返回了正确的非零值。为什么函数无法正确返回此结果?当for循环中的乘法/加法返回错误时,返回的I数组为329 x 1,并在行中包含值 Function Gen

我正在编写一个Excel VBA函数,该函数返回一个数组以在其他函数中使用。当我在Excel工作表中测试GenerateBlendedReturnSeries函数并使用ctlr shift enter查看完整结果时,整个数组都是零。然而,奇怪的是,当我检查Debug.Print BlendedReturnSeriesArray300,1时,返回了正确的非零值。为什么函数无法正确返回此结果?当for循环中的乘法/加法返回错误时,返回的I数组为329 x 1,并在行中包含值

Function GenerateBlendedReturnSeries(AccountID1 As String, Account1Proportion As Double, _
     Optional ByVal AccountID2 As String, Optional ByVal Account2Proportion As Double, _
     Optional ByVal AccountID3 As String, Optional ByVal Account3Proportion As Double) As Variant 'Vs. As Double()

' CODE IN BETWEEN

Dim BlendedReturnSeriesArray As Variant
    ReDim BlendedReturnSeriesArray(ArraySize, 1)

    Debug.Print (ArraySize)

    On Error Resume Next
    For i = 0 To UBound(BlendedReturnSeriesArray)

        BlendedReturnSeriesArray(i, 1) = _
          Account1PeriodReturnSeriesArray(i, 1) * Account1Proportion _
        + Account2PeriodReturnSeriesArray(i, 1) * Account2Proportion _
        + Account3PeriodReturnSeriesArray(i, 1) * Account3Proportion
        'Debug.Print (BlendedReturnSeriesArray(i, 1))
        'Debug.Print (i)

    Next i

    On Error GoTo 0

    Debug.Print (BlendedReturnSeriesArray(300, 1))

    GenerateBlendedReturnSeries = BlendedReturnSeriesArray 'BlendedReturnSeriesArray

End Function
除非你这么做

ReDim BlendedReturnSeriesArray(1 To ArraySize, 1 To 1)
或者您正在使用选项Base 1。这不是一个好主意,返回数组的大小将为0到ArraySize,0到1,因此您可能只看到数组的第一列,即工作表上的零索引。如果将公式展开为包含两列,您将看到缺少的数字。

ArraySize从何而来?或Account1PeriodReturnSeriesArray等?ArraySize来自行。计算从索引获得的范围,使用AccountID进行匹配。Account1PeriodReturnSeriesArray是维度ArraySize x 1的一个变体,它来自Account1PeriodReturnSeries范围,您可以从索引中获得,并使用AccountID进行匹配。我省略了这段代码,因为正如Debug.Print显示的那样,最终的数组按照预期工作,除非我尝试返回它。