+ Reply to Thread
Results 1 to 7 of 7

Can't get LinEst to work in VBA with an array.

Hybrid View

  1. #1
    Registered User
    Join Date
    09-28-2016
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    8

    Can't get LinEst to work in VBA with an array.

    Hello,
    This is part of my code below, Correl works for the same array, but LinEst doesn't. I have noticed that LineEst keeps changing to lower case automatically. I am using Excel 2003. I have tried looking up other similar issues, but there isn't enough information in any of the posts I read, as to what the problem was.
    Can anyone see why this is not working?
    Thanks,

    Dim correlation As Double
    Dim arrP As Variant, arrW As Variant
    Dim lin As Variant
    arrP = Array(p(a), p(b), p(c))  'set initial arrays as GSL 1, 2, 3
    arrW = Array(w(a), w(b), w(c))
    n = 2
        
    CheckCorrelation:
       
        correlation = Application.WorksheetFunction.Correl(arrP, arrW) 'calculate correlation
        lin = Application.WorksheetFunction.linest(arrP, arrW, False) 'calculate slope
    Please follow forum rules and use code tags when posting code
    Last edited by jeffreybrown; 10-01-2016 at 08:50 PM. Reason: Added code tags

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

    Re: Can't get LinEst to work in VBA with an array.

    This forum can be very strict about putting your code within code tags. You should edit your post to include code tags, to avoid getting your topic locked.
    I cannot look at this now (don't have Excel on this computer), but I will try to look at it later. In the meantime, can you provide any details beyond "it doesn't work"? Does it give you a compile error? runtime error? runs but the regresion is incorrect? Have you stepped through the code and identified the statement where it goes wrong?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Can't get LinEst to work in VBA with an array.

    Your arrays are not initialized in the code you posted.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    09-28-2016
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Can't get LinEst to work in VBA with an array.

    Hi,
    The error is, lin doesn't return a value. I have stepped through the code, and it just goes past this line.

    Sorry I am not sure how to initialize the array?

  5. #5
    Registered User
    Join Date
    09-28-2016
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Can't get LinEst to work in VBA with an array.

    I cut it out of the code in the question, but I had already put in values for p(a), p(b), p(c) - if that's what you mean by initialize the array.

    correlation returns a value, so the array works here.
    buy lin doesn't return a value

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

    Re: Can't get LinEst to work in VBA with an array.

    I cannot recreate the error. As shg noted, the code block you posted does not tell us what is in p and w. If I assume something reasonable and simple for these values, then the code runs without error. For example, if I change the arrP and arrW assignmenet statements:
    arrP = Array(2,4,6) 'p(a), p(b), p(c))  'set initial arrays as GSL 1, 2, 3
    arrW = Array(1,2,3) 'w(a), w(b), w(c))
    The code runs without error and correctly gives me slope=2 and intercept=0.

    I don't think the problem is with the LinEst statement. It appears to me that the problem is something in the data being fed to the Linest statement. At this point, I would suggest that you use the locals window and or watch window and or the immediate window (see here http://www.cpearson.com/Excel/DebuggingVBA.aspx or other page if you are unfamiliar with these debugging tools) to see what is stored in p(), w(), arrp, and arrw to identify what is wrong with the input data.

    Off the top of my head, things that Correl() will handle that LinEst() will not would be things like blanks, text, or other non-numeric data.

  7. #7
    Registered User
    Join Date
    09-28-2016
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Can't get LinEst to work in VBA with an array.

    Thanks again Mr Shorty - the locals window helped solve the problem. I didn't realise that the value of lin will actually be at lin(1) because lin was a variant to make LinEst work.

+ 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. [SOLVED] Linest Function - Unable to get LinEst property of the WorksheetFunction class
    By fbs13 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 07-25-2013, 09:55 AM
  2. [SOLVED] Linest function storable in an array??
    By bezesi in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-22-2013, 11:00 AM
  3. [SOLVED] LINEST with an autopopulating array within VBA
    By grimster85 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-22-2013, 08:21 PM
  4. Question on exact formulae of numbers in linest array
    By qinghe.yin in forum Excel General
    Replies: 0
    Last Post: 05-03-2011, 12:15 AM
  5. LINEST array formula not working
    By mouseclicker in forum Excel General
    Replies: 2
    Last Post: 10-12-2010, 08:29 PM
  6. Referring to an array column or row of LinEst function
    By HammerTime in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2010, 01:11 AM
  7. [SOLVED] How do I extract a single value from the linest array result?
    By hil in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-12-2006, 07:40 AM

Tags for this Thread

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