Result Screenshot: ERROR

问题描述 投票:0回答:1

结果截图。ERROR:

Result Screenshot: ERROR

样本数据截图。

Sample data Screenshot

我是VBA新手,现在我被一个错误卡住了。我想以股票价格为参数计算协方差,并返回一个n*n的数组(n=其参数的列数)。我想不出我在以下代码中的错误。

Function covarmat(prices As Range) As Variant
    Dim i As Integer, j As Integer
    Dim n As Integer

    n = prices.Columns.Count

    Dim resultarray()

    ReDim resultarray(n, n)
    Dim f  'as a proxy for return variant
    Dim basicarray() 'to extract columns and calculate returns
    ReDim basicarray(prices.Rows.Count, prices.Columns.Count)                                               Tried this separately! Works perfect!

    For j = 1 To prices.Columns.Count
        For i = 1 To prices.Rows.Count
            basicarray(i, j) = prices(i + 1, j) / prices(i, j) - 1
        Next i
    Next j
    f = basicarray

    For i = 1 To n
        For j = 1 To n
            resultarray(i, j) = Application.Covariance_S(f.columns(i), f.Columns(j))
        Next j
    Next i

covarmat = resultarray
End Function
excel vba user-defined-functions covariance
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.