Friday, August 6, 2010

VBA for Variance-Covariance Matrix

Function VarCov(rng As Range) As Variant
   
    Dim i As Integer
    Dim j As Integer
    Dim colnum As Integer
    Dim matrix() As Double
   
    colnum = rng.Columns.Count
    ReDim matrix(colnum - 1, colnum - 1)
       
    For i = 1 To colnum
        For j = 1 To colnum
            matrix(i - 1, j - 1) = Application.WorksheetFunction.Covar(rng.Columns(i), rng.Columns(j))
        Next j
    Next i

    VarCov = matrix

End Function


4 comments:

  1. I know you posted this a while ago, but you just saved me a LOT of time, thx, you are the man

    ReplyDelete
  2. i love reading this article so beautiful!!great job!
    Company Formation Singapore

    ReplyDelete
  3. dude you dont explain how to input.

    ReplyDelete