Sub testCov()
Rng2 = Sheets("20 Asset Model").Range("b3:f48")
Dim covMatrix() As Variant
ReDim covMatrix(1 To Rng2.Columns.Count, 1 To Rng2.Columns.Count)
Call constructCovMatrix(Rng2, covMatrix)
MsgBox (covMatrix)
End Sub
Sub constructCovMatrix(rng, ByRef covMatrix)
'@rng The Range of the return series.
Dim i As Integer
Dim j As Integer
For i = 1 To rng.Columns.Count
For j = 1 To rng.Columns.Count
covMatrix(i, j) = Application.WorksheetFunction.Covar(rng.Columns(i), rng.Columns(j))
Next
Next
End Sub
代碼停在ReDim行,表示缺少對象。爲什麼是這樣?由於子程序VBA中的ReDim
'Rng2 =表(「20資產模型」)。範圍(「b3:f48」)'是一個數組而不是範圍。嘗試設置Rng2 =表格(「20資產模型」)。範圍(「b3:f48」)'。 – user3357963