Below is a user defined function i put together to gain flexibly outputting correlation matrix values to a location of my choosing. The problem is, when I enter my data range and click OK I only get one value. The value is a "1" which is a good sign (lies on the diagonal), but when I try to expand the selection I get error values. How can I expand the function output value to cover the entire matrix space?
I did include an 'output' section near the bottom of my code which mimics another UDF output format. How can I see all the correlation values in my matrix?
I did include an 'output' section near the bottom of my code which mimics another UDF output format. How can I see all the correlation values in my matrix?
Code:
Public Function CorrelMatrix(rng As Range) As Variant
Dim i As Integer
Dim j As Integer
Dim num_Cols As Integer: num_Cols = rng.Columns.Count
Dim num_Rows As Integer: num_Rows = rng.Rows.Count
Dim CMat() As Double
ReDim CMat(num_Cols, num_Cols)
For i = 1 To num_Cols
For j = 1 To num_Cols
CMat(i, j) = Application.WorksheetFunction.Correl(rng.Columns(i), rng.Columns(j))
Next j
Next i
CorrelMatrix = CMat
'Output
For i = 1 To num_Cols
For j = 1 To num_Cols
'CMat(i, j) =
Next j
Next i
End Function