i'm trying to make a function that calculates the LINEST for a different ranges of data within a big two dimensional array, everything works fine except for when i need to use the function LINEST within a loop and i get the error #VALUE. but all seems to work fine for the first iteration. I'm new to EXCEL and VBA so i'm still a little lost is there any way i can solve this problem Thank you all for your guidance and help in advance :)
Function prediction(mytable As Range) As Variant
Dim i, j, k, row, col, cnt, num, slide, z, marge, x, y As Integer
Dim coef As Double
Dim fin As Integer
Dim eurusd() As Variant
Dim TH() As Variant
Dim linest() As Variant
Dim known() As Variant
Dim yconnu() As Variant
Dim xconnu() As Variant
Dim list() As Variant
row = mytable.Rows.count
col = mytable.Columns.count
cnt = row
marge = 730
num = 730
slide = 60
z = 16
x = (row / slide) - 1
y = 1
ReDim eurusd(1 To row)
ReDim TH(1 To row)
ReDim linest(1 To 5, 1 To col)
ReDim known(1 To row, 1 To col - 1)
ReDim yconnu(1 To marge)
ReDim xconnu(1 To marge, 1 To col - 1)
ReDim list(1 To marge)
'main code body-----------------------------
For i = 1 To cnt
eurusd(i) = mytable(i, col)
For j = 1 To col - 1
known(i, j) = mytable(i, j)
Next j
Next i
'BIG loop-------------------------------
For y = 1 To 10
For i = 1 To marge
yconnu(i) = eurusd(cnt - num + i)
For j = 1 To col - 1
xconnu(i, j) = known(cnt - num + i, j)
Next j
Next i
If y = 1 Then
yconnu = Application.WorksheetFunction.Transpose(yconnu)
End If
linest = Application.WorksheetFunction.linest(yconnu, xconnu, True, True)
coef = linest(1, 17)
k = cnt - num
For i = k - slide To k
TH(i) = coef
For j = 0 To z - 1
TH(i) = TH(i) + known(i, j + 1) * linest(1, z - j)
Next j
Next i
num = num + slide
Next y
'END OF BIG LOOP
TH = Application.WorksheetFunction.Transpose(TH)
prediction = TH
End Function