Hi, I need a little help. I developed two functions that work perfectly separately, but gives an error message when using them in a nested way. My intention is to use {=varcovar(Step1(datarange))} [ctrl+shift+enter].
Funtion Step1 takes a range as an input, extracts the mean from each column, divides by the standard deviation of each column, and returns a range of the same size as the original containnig the transformed values. The relevant bits are:
Function Step1(rango As Range)
Dim i, j As Integer
Dim numRows As Integer
Dim numCols As Integer
numRows = rango.Rows.Count
numCols = rango.Columns.Count
ReDim Result(1 To numRows, 1 To numCols) As Double
ReDim x(1 To numRows)
Dim B As Double
Dim C As Double
B = 0
C = 0
For i = 1 To numCols
B = Application.WorksheetFunction.Average(rango.Columns(i))
C = Application.WorksheetFunction.StDev(rango.Columns(i))
For j = 1 To numRows
x(j) = (rango(j, i) - B) / C
Result(j, i) = x(j)
Next j
Next i
Step1 = Result
End Function
The second function is varcovar and takes a range as an input and calculates a (square) covariance matrix. The relevant bit is
Function VarCovar(rng As Range) As Variant
'All operations go here
VarCovar = matrix
End Function
The input in varcovar should be the transformed range obtained from Step1 function.
I hope some one can help me. The best for you all.
Funtion Step1 takes a range as an input, extracts the mean from each column, divides by the standard deviation of each column, and returns a range of the same size as the original containnig the transformed values. The relevant bits are:
Function Step1(rango As Range)
Dim i, j As Integer
Dim numRows As Integer
Dim numCols As Integer
numRows = rango.Rows.Count
numCols = rango.Columns.Count
ReDim Result(1 To numRows, 1 To numCols) As Double
ReDim x(1 To numRows)
Dim B As Double
Dim C As Double
B = 0
C = 0
For i = 1 To numCols
B = Application.WorksheetFunction.Average(rango.Columns(i))
C = Application.WorksheetFunction.StDev(rango.Columns(i))
For j = 1 To numRows
x(j) = (rango(j, i) - B) / C
Result(j, i) = x(j)
Next j
Next i
Step1 = Result
End Function
The second function is varcovar and takes a range as an input and calculates a (square) covariance matrix. The relevant bit is
Function VarCovar(rng As Range) As Variant
'All operations go here
VarCovar = matrix
End Function
The input in varcovar should be the transformed range obtained from Step1 function.
I hope some one can help me. The best for you all.
Last edited: