I'm working on a code which generates a covariance matrix for some data, specifically 2 columns (i.e a 2x2 matrix). What's happening is that the code worked before perfectly and now it doesn't without any significant change (just made a function with it). The code runs, shows the MsgBox and when it has to calculate the matrix it just goes to the end skipping all the lines.
Code:
Function GetBaseRate(ranges() As Variant, Days() As Variant) As Object
' Get Data from Data Base
' Use this array to render the bucket titles
Dim Covariance As Object
Dim Cov As Variant
Set Covariance = CreateObject("Scripting.Dictionary")
Set sh = ThisWorkbook.Worksheets("AUX")
' Get Covariance for each range
limit = UBound(ranges)
MsgBox limit
For i = 0 To limit
MsgBox ranges(i) & " - " & Days(i)
Next i
For i = 0 To limit
sh.UsedRange.ClearContents
' query for each day
MsgBox ranges(i) & " - " & Days(i)
Query = "SELECT TASA FROM [NVSSQLBI].[MESA].[dbo].[CURVA_BASE_CLP_ON_FECHA] WHERE DIAS ='" & Days(i) & "' AND FECHA > '" & DateAdd("yyyy", -5, Date) & "' ORDER BY FECHA DESC"
Query2 = "SELECT TASA FROM [NVSSQLBI].[MESA].[dbo].[CURVA_BASE_CLF_ON_FECHA] WHERE DIAS ='" & Days(i) & "' AND FECHA > '" & DateAdd("yyyy", -5, Date) & "' ORDER BY FECHA DESC"
aux = BDconexion2(Query)
aux2 = BDconexion2(Query2)
Count = 1
' CLP
For Each element In aux
sh.Cells(Count, 1).Value = element
Count = Count + 1
Next element
Count = 1
' UF
For Each element In aux2
sh.Cells(Count, 2).Value = element
Count = Count + 1
Next element
' Generate Cov
Cov = VarCov(Range("A1:B" & Count - 1))
'Debug.Print Cov(0, 0) & " - "
Covariance.Add CStr(ranges(i)), Cov
Next i
GetBaseRate = Covariance
End Function
It make a sql query to a database and puts the data into a Worksheet for later calculation of it's covariance matrix. The code which calculates the matrix is this one:
Function VarCov(rng As Range) As Variant
' Returns covariance matrix
Dim i As Integer
Dim j As Integer
Dim colnum As Integer
Dim Matrix() As Double
colnum = rng.columns.Count
ReDim Matrix(colnum, colnum)
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
I have no idea why It does not work, I'm working with the previous version and It doesn't fail at all. After debugging, the fail point is located at
Matrix(i - 1, j - 1) = Application.WorksheetFunction.Covar(rng.columns(i), rng.columns(j))
line which used to work perfectly, and now It does not throw any error just goes to the line after the function (a MsgBox saying "Done")