+ Reply to Thread
Results 1 to 6 of 6

Converting a polynomial LINEST formula to VBA

  1. #1
    Registered User
    Join Date
    12-11-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Converting a polynomial LINEST formula to VBA

    I have successfully implemented the appropriate =LINEST() formula for a polynomial regression on my spreadsheet, but when I try to implement the same calculation in VBA I can't figure out the correct syntax. Any help would be appreciated.

    Formula on Sheet: =LINEST(M45:M59,J45:J59^{1,2},TRUE, TRUE)

    Code in VBA: regressionVariables = WorksheetFunction.LinEst(yValues, xValues ^ Array(1, 2), True, True)

    Arrays are included for type reference and this is not the order of code
    ReDim xValues(1 To UBound(bondDataArray, 2)) As Variant
    ReDim yValues(1 To UBound(bondDataArray, 2)) As Variant

    Keep in mind that yValues and xValues are single dimension arrays and the error is a type mismatch error related to the LinEst function. I just can't find a good example of the correct syntax for a polynomial implementation, where straight line examples are all over.

    Thanks.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,517

    Re: Converting a polynomial LINEST formula to VBA

    VBA does not know how to handle this syntax the way Excel does xValues ^ Array(1, 2)
    In Excel this syntax creates a two dimensional array where known_x(i,1)=xvalues(i) and known_x(i,2)=xvalues(i)^2. To make this work in VBA, you first must create the complete known_x matrix from the xvalues vector. This should be easy enough using a For Next or For Each Next loop.

    In building the two dimensional matrix that will be known_x, you will probably run into some problem with known_y. I can't remember which way it goes, but a one dimensional array is either horizontal or vertical, and I can't remember which it is. When you build the 2D known_x array, if it's "orientation" is not the same as the known_y array, I believe it will give you an error or just crash. I usually dimension known_y with 2 indices (Dim yvalues(n,1) or yvalues(1,n)) depending on which way I orient known_x to make it unambiguous.

  3. #3
    Registered User
    Join Date
    12-11-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Converting a polynomial LINEST formula to VBA

    Thanks for the response...

    I have noticed many examples using the WorsheetFunction.Transpose() function which looks to correct the issue you are referring to, but none of the examples actual compile for me. I keep getting a type mismatch error.

    I chose to just build the formula as a string and pass my it through to the evaluate function in Excel and go on down the road. Maybe someone else will find the correct implementation, but I will post my workaround for those out there in the same situation.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    12-11-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Converting a polynomial LINEST formula to VBA

    removed duplicate post

  5. #5
    Registered User
    Join Date
    05-31-2025
    Location
    DK
    MS-Off Ver
    2021
    Posts
    1

    Re: Converting a polynomial LINEST formula to VBA

    I have made a vba function ussing linest and handles emty cells in y range (sorry for a little danish in the function):

    Function FitPoly(Range_y As Range, Range_x As Range, gr As Integer, Optional arg3 As Boolean = True, Optional arg4 As Boolean = False) As Variant
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim x() As Variant
    Dim y() As Variant

    Dim First_row As Integer
    Dim Last_row As Integer
    Dim Antal_Rows As Integer
    Dim Antal_Blank_y As Integer

    First_row = Range_x.Row
    Last_row = First_row + Range_x.Rows.Count - 1
    Antal_Rows = Last_row - First_row + 1

    j = 0
    For i = 1 To Antal_Rows
    If ((Not IsEmpty(Range_y.Cells(i, 1))) And (Not IsEmpty(Range_x.Cells(i, 1)))) Then
    j = j + 1
    End If
    Next i

    ReDim x(1 To j)
    ReDim y(1 To j)

    Dim pj() As Variant
    ReDim pj(1 To gr)

    For k = 1 To gr
    j = 0
    For i = 1 To Antal_Rows
    If ((Not IsEmpty(Range_y.Cells(i, 1))) And (Not IsEmpty(Range_x.Cells(i, 1)))) Then
    j = j + 1
    If k = 1 Then
    y(j) = Range_y.Cells(i, 1)
    End If
    x(j) = Range_x.Cells(i, 1) ^ k
    End If
    Next i
    pj(k) = x
    Next k

    Dim z() As Variant

    z = WorksheetFunction.LinEst(y, pj, arg3, arg4)
    FitPoly = z

    End Function

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,051

    Re: Converting a polynomial LINEST formula to VBA

    Quote Originally Posted by pmjef View Post
    I have made a vba function ussing linest and handles emty cells in y range (sorry for a little danish in the function):

    .....
    1. You may not have noticed but this thread is over 12 years old. Thanks for the input though.

    2. please use code tags when posting code (see my foot note)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1