+ Reply to Thread
Results 1 to 3 of 3

LinEst for polynomial regression in VBA - error...

  1. #1
    Registered User
    Join Date
    05-17-2014
    MS-Off Ver
    Excel 2013
    Posts
    2

    Question LinEst for polynomial regression in VBA - error...

    Hello everyone,

    I am trying to use the LinEst function in VBA to get the best fit coefficients for some data fitted by a polynomial curve. I can get this for a single variable, but not multi-variable.

    My code is:

    Please Login or Register  to view this content.

    This approach seems to work when I do it via array formulas directly in Excel, but for some reason it is not working in VBA.

    The error message is: "Run-time error '1004': Unable to get the LinEst property of the WorksheetFunction class"

    (p.s. this is not just because 250^4 is a large number: I have tried this with a shorter set of arrays, but the issue does not go away...)


    Thanks in advance for any help or suggestions. I've tried all of Google and am completely stuck.
    Last edited by IDidNotWantToRegiste; 05-17-2014 at 09:02 PM.

  2. #2
    Registered User
    Join Date
    05-17-2014
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: LinEst for polynomial regression in VBA - error...

    Still unsolved

    This post was originally fixing a typo in my explanation that I now realise I could edit directly - so have done that instead now.
    Last edited by IDidNotWantToRegiste; 05-17-2014 at 09:04 PM.

  3. #3
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: LinEst for polynomial regression in VBA - error...

    Quote Originally Posted by IDidNotWantToRegiste View Post
    My code is:
    Please Login or Register  to view this content.
    [....]
    The error message is: "Run-time error '1004': Unable to get the LinEst property of the WorksheetFunction class"
    That is just VBA's way of indicating, in this context, that LinEst returned an error.

    The key to the error is in the help page for LinEst, to wit:

    Please Login or Register  to view this content.
    With the declaration DependentVariable(1 To 250), "known_y" is treated as a single row.

    Therefore, the declaration for "known_x" must be LinestArray(1 to 4, 1 To 250), changing references in the For-loop accordingly.

    Alternatively and perhaps easier, change "known_y" to DependentVariable(1 To 250, 1 to 1), changing references in the elided code accordingly.

    But you might have another programming mistake.

    Unless the x-axis values are 1 to 250, the values assigned to LinestArray in the For-loop should be:

    Please Login or Register  to view this content.
    where the array "x" constains the actual x-axis values, which must be input.

    PS: You can avoid the VBA runtime error by using Application.Index and Application.LinEst (deprecated, I believe) and assigning the result to a type Variant variable; or better: by inserting the statement On Error Resume Next before the statement that uses WorksheetFunction.Index. In either case, you must handle the error condition.
    Last edited by joeu2004; 05-18-2014 at 11:14 AM. Reason: better grammar

  4. #4
    Registered User
    Join Date
    05-17-2014
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: LinEst for polynomial regression in VBA - error...

    Perfect - thank you!! That all works now.

    I did look a the help page for LinEst, but as usual the subtleties were lost on me. I had not realised that Array(1 to 250) would be considered a row rather than a column.

    Thanks again!

  5. #5
    Registered User
    Join Date
    05-17-2014
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: LinEst for polynomial regression in VBA - error...

    Perfect - thank you!! That all works now.

    I did look a the help page for LinEst, but as usual the subtleties were lost on me. I had not realised that Array(1 to 250) would be considered a row rather than a column.

    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. LINEST for specifying polynomial regression function
    By InderpalHothi in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-27-2014, 03:11 PM
  2. [SOLVED] trend/linest regression error
    By MrShorty in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-07-2013, 01:16 PM
  3. Converting a polynomial LINEST formula to VBA
    By AARONWEBSTER in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-12-2012, 12:23 PM
  4. Replies: 5
    Last Post: 06-25-2009, 10:25 AM
  5. [SOLVED] How to clear "linest ( ) function error " in regression analysis?
    By dev in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 08-21-2005, 04:05 PM

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