VBA范围输入值错误

时间:2017-10-07 18:23:12

标签: vba excel-vba function excel

我试图通过读取一个Range来计算输出,然后将其作为一个数组使用。我的公式返回值错误。如果将代码写为sub而不读取数组,则选择Range(“”)代码可以正常工作。也许你可以看到m错误?

Public Function pfsd2A(sdInp As Range, wInp As Range, corInp As Double)

'only for two assets
Dim matrix() As Variant
Dim sd As Variant
Dim w As Variant
Dim cor As Double 'should be a matrix for more than 2 assets!!
Dim out As Variant

sd = sdInp.Value
w = sdInp.Value
cor = corInp
assetnum = UBound(sd)

ReDim matrix(1 To assetnum, 1 To assetnum)

Debug.Print sd(1, 1)

For j = 1 To assetnum
    For i = 1 To assetnum
        matrix(i, j) = w(j, 1) ^ 2 * sd(j, 1) ^ 2 + w(i, 1) ^ 2 * sd(i, 1) ^ 2 + 2 * w(i, 1) * w(j, 1) * sd(i, 1) * sd(j, 1) * cor
        Cells(i, j) = matrix(i, j)
    Next i
Next j

VarPf = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MMult(Application.WorksheetFunction.Transpose(w), matrix), w)

pfsd2A = Sqr(VarPf(1))

End Function

0 个答案:

没有答案