Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
Is it possible to post your workbook (sanitised if necessary) so we don't have to re-create your issue? – brettdj 8 hours ago

1 Answer

My guess is that either the yconnu or the xconnu isn't being updated as you're expecting. Without some sense of the data being used, the best answer is going to be "learn to debug VBA code". Set a breakpoint at that line and check the values being passed to it. Here are a couple of links to get you started:

http://www.cpearson.com/excel/DebuggingVBA.aspx

and

http://msdn.microsoft.com/en-us/library/office/aa164821(v=office.10).aspx

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.