Public Function APortfolioReturnsXL1(Returns As Range, Weights As Range) As Double()
On Error GoTo errHandler
Dim arrReturns() As Double
Dim arrWeights() As Double
Dim TotRows As Double
Dim TotCols As Double
Dim RowCtr As Double
Dim Colctr As Double
Dim WgtColctr As Double
Dim WgtRows As Double
TotRows = Returns.Rows.Count
TotCols = Returns.Columns.Count
WgtRows = Weights.Rows.Count
ReDim arrReturns(1 To TotRows, 1 To TotCols)
For RowCtr = 1 To TotRows
For Colctr = 1 To TotCols
arrReturns(RowCtr, Colctr) = Val(Returns.Cells(RowCtr, Colctr).Value)
Next
Next
ReDim arrWeights(1 To WgtRows, 1 To 1)
For WgtColctr = 1 To WgtRows
arrWeights(WgtColctr) = Val(Weights.Cells(WgtColctr, 1).Value)
Next
APortfolioReturnsXL1 = APortfolioReturns(arrReturns(), arrWeights())
Exit Function
errHandler:
MsgBox "An error has occurred." & vbCrLf & Err.Description & vbCrLf & CStr(Err.Number)
End Function
我得到
错误下标超出范围
。
它不在此行进入循环:
For WgtColctr = 1 To WgtRows
arrWeights(WgtColctr) = Val(Weights.Cells(WgtColctr, 1).Value)
Next
答案 0 :(得分:0)
您正在尝试将值分配给 2D数组。
使用:arrWeights(WgtColctr, 1) = Val(Weights.Cells(WgtColctr, 1).Value)
代替: arrWeights(WgtColctr) = Val(Weights.Cells(WgtColctr, 1).Value)