Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50298

How to call a whole column from an array

$
0
0
Hi

I am trying to calculate the variance-covariance from a matrix containing historical stock info.

Code:

Function Smatrix(StockMatrix() As Double) As Double
Dim arrResult() As Double
Dim i As Long, j As Long
Dim rs As Long, re As Long
Dim h As Long

rs = LBound(StockMatrix, 2)
re = UBound(StockMatrix, 2)
h = UBound(StockMatrix, 1) - LBound(StockMatrix, 1) + 1

ReDim arrResult(1 To h, 1 To h)

For i = 1 To h
    For j = 1 To h
        With Application.WorksheetFunction

'---------------------------------------------------------------------------
'***************Problem is here*************
            dArrResult(i, j) = .Covar(StockMatrix( (rs:re) , i), StockMatrix( (rs:re) , j))
'---------------------------------------------------------------------------

        End With
    Next j
Next k
Smatrix = arrResult
End Function

The different columns in the StockMatrix represent the various stocks, and the rows the historical data. I however cannot figure out how to call a whole column from the StockMatrix. Above I have tried with rs:re (abbr. for row_start:row_end). I have tried to google a solution, and it seems a lot of people use the WorksheetFunction Index, but I have a hard time understanding the use of Index in VBA--why not refer to array elements by just using their indices directly, why would you want to write the extra word Index?

I have been basing my code on some code I found on the internet:

Code:

dArrResult(j, k) = .Covar(.Index(dArrData, 0, j), .Index(dArrData, 0, k))
Where dArrData is the same as my Stockmatrix. So here it seems that a whole column is called by just using "0", but in my mind Index(dArrData, 0, j) simply refers to the element(singular) located at (0 , j)--and once again, why use the Index function, why not just go dArrData(0,j)?

Thanks for any help :-)

Viewing all articles
Browse latest Browse all 50298

Latest Images

Trending Articles



Latest Images