axupaxu
04-23-2008, 06:18 AM
I have data on stock prices in columns b-f, rows 2-251. I then calculate the returns on these prices and store them in an array called vReturns.
Then I want to calculate the covariances between the the stock returns, i.e. the covariances between columns in vReturns and store these in a 5x5 array called vMatrix.
However, unlike with the range.columns property, I am unable to specify a certain column within vReturns. I get "Invalid qualifier". Please advice. Entire code below: The faulty code is marked bold:
Public Function CovarMatrix(Theprices As Range) As Variant
Dim vRow As Integer
Dim vColumn As Integer
Dim vMatrix() As Variant
Dim vReturns() As Variant
ReDim vReturns(1 To Theprices.Rows.Count, 1 To Theprices.Columns.Count)
ReDim vMatrix(1 To Theprices.Rows.Count, 1 To Theprices.Columns.Count)
For vRow = 1 To Theprices.Rows.Count - 1
For vColumn = 1 To Theprices.Columns.Count
vReturns(vRow, vColumn) = (Theprices(vRow, vColumn).Value / Theprices(vRow + 1, vColumn).Value)-1
Next vColumn
Next vRow
vRow = 1
vColumn = 1
For vRow = 1 To Theprices.Columns.Count
For vColumn = 1 To Theprices.Columns.Count
vMatrix(vRow, vColumn) = Application.WorksheetFunction.Covar(vReturns.Columns(vRow), vReturns.Columns(vColumn))
Next vColumn
Next vRow
CovarMatrix = vMatrix
End Function
Then I want to calculate the covariances between the the stock returns, i.e. the covariances between columns in vReturns and store these in a 5x5 array called vMatrix.
However, unlike with the range.columns property, I am unable to specify a certain column within vReturns. I get "Invalid qualifier". Please advice. Entire code below: The faulty code is marked bold:
Public Function CovarMatrix(Theprices As Range) As Variant
Dim vRow As Integer
Dim vColumn As Integer
Dim vMatrix() As Variant
Dim vReturns() As Variant
ReDim vReturns(1 To Theprices.Rows.Count, 1 To Theprices.Columns.Count)
ReDim vMatrix(1 To Theprices.Rows.Count, 1 To Theprices.Columns.Count)
For vRow = 1 To Theprices.Rows.Count - 1
For vColumn = 1 To Theprices.Columns.Count
vReturns(vRow, vColumn) = (Theprices(vRow, vColumn).Value / Theprices(vRow + 1, vColumn).Value)-1
Next vColumn
Next vRow
vRow = 1
vColumn = 1
For vRow = 1 To Theprices.Columns.Count
For vColumn = 1 To Theprices.Columns.Count
vMatrix(vRow, vColumn) = Application.WorksheetFunction.Covar(vReturns.Columns(vRow), vReturns.Columns(vColumn))
Next vColumn
Next vRow
CovarMatrix = vMatrix
End Function