+ Reply to Thread
Results 1 to 8 of 8

VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

  1. #1
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    Hi,
    I need help to use VBA to copy the equation from the graph to a cell E30, and format the equation so that it can be calculated pending on X values. Next if the numbers in Y-corrdinate changes,
    I would like the graphic, labels and equation update and paste the new equation to a different cell E45, and refomat the equation. the first equation in E30 will be kept. In some case the numbers for X-coordinate changes or both X and Y coordinates may have different numbers to generate different curves and equation. But I would like always add the new equation to a new cell (E31)below the previous one.

    Can your experts advise? I am a dump bell in VBA.


    thanks.
    Attached Files Attached Files
    Last edited by Rocky2013; 11-08-2012 at 09:08 PM.

  2. #2
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    Hi, I am still looking for solution. Please, Thanks.

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

    Re: VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    Does this all have to be done using the graph trendlines to get the equations, then VBA to copy the equations to a spreadsheet? My personal preference with this sort of thing is to use the LINEST() function to generate the equation coefficients directly in the spreadsheet. One LINEST() function for each data set will give you the best fit curve for each data set.

  4. #4
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    Hi,
    Many thanks of your reply. It does not need to use the graphic trendlines to get the equation beacuse
    this is what I know how to get the equation. The reason I need an equation becuase the X value is every 50 units apart.
    I need to be able to enter any value between two X values (e.g 105) then give me Y value.

    This is a curve, I like to use power of 5, precision of 5 decimal places to keep the equaltion as accurate as possible to
    match the Y vales in the table. Is LINEST function for a straight line? How to make a fit curve using function?

    I have been trying find a way how to generate an equation from X and Y table using function or VBA.
    If you can use a function to generate the equation same as graph trendline. Then that is perfect.
    Please note the X and Y table will change dynamicly pending on another calulations which I did not show in the attached file.
    My guess the equation is pending on values in X-Y table.


    Best Regards,
    Last edited by Rocky2013; 11-07-2012 at 10:05 PM.

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

    Re: VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    Is LINEST function for a straight line?
    Yes, but it can do so much more than straight lines. The LINear in LINEST() is referring to linear equations as you would learn them in a linear algebra course (I'm sure a quick internet search should yield any number of tutorials introducing linear equations). For our purposes here, every one of the trendline options (except moving average) available in the chart trendlines is a linear function (or can be manipulated to be linear), so you can make a spreadsheet to make any of those functions using LINEST(). If you missed the help file, here's an online version http://office.microsoft.com/en-us/ex...010069838.aspx and it describes one way to use LINEST() to get polynomials in the paragraph just before example 1.

    Please note the X and Y table will change dynamicly pending on another calulations
    This is one reason I like to use LINEST() for regressions -- as the data updates, the spreadsheet calculates the new coefficients automatically.

  6. #6
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    Thanks for the help. I am going to try for myself first to learn and try and errors.
    If I get lost and could not make it work, I may come back for help.

    Best Regards,

  7. #7
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    Hi,
    I goggle the web try to find help how to take the values from the XY table, then give me an equation. I found topics on LINEST, TREND, LOGEST, I am so confuse understanding.
    There are some examples in help, but I do not see any result with an equation, but a number.

    In my example, I like see LINEST function can do this.......
    =LINEST(C2:C20,B2:B20,5,4) B2:B20 is the list of Y values from the XY table; B2:B20 is the list of X values from the XY table; 5 is the power of polynomnal; 4 is the scientific precision from tredline label. Then an equation will return in the cell as "-2.048079E-18x6 + 7.107127E-15x5 - 9.399747E-12x4 + 5.898487E-09x3 - 1.891143E-06x2 + 7.719149E-04x + 2.546328E+00". It is nice to use VBA to convert to "-2.048079*(10^-18)*(X^6)+7.107127*(10^-15)*(X^5)-9.399747*(10^-12)*(X^4)+5.898487*(10^-9)*(X^3)-1.891143*(10^-6)*(X^2)+7.719149*(10^-4)*X+2.546328". Rather to generate a chart => display the trendline => copy and paste to a cell => rewrite the equation.

    I know I will have a hard time to understand and create the equation from XY table. Best if there are examples. Maybe take the XY table in "graph 1" in example file, then add LINEST to come up with an equation same as the trendline in the graph.
    Last edited by Rocky2013; 11-08-2012 at 10:14 PM.

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

    Re: VBA: Copy equation from graph to cell; update all if numbers in Y-coordinate changes

    =LINEST(C2:C20,B2:B20,5,4)
    You are misunderstanding the arguments for the LINEST() function. The order of the polynomial is hidden in the known x argument. The 3rd and 4th arguments are booleans that simply tell LINEST whether you want to force the y intercept through 0 (3rd argument is FALSE, TRUE calculates constant term normally) and whether you want LINEST() to return the extra statistics (4th argument is TRUE to return values, FALSE to not return).

    I found topics on LINEST, TREND, LOGEST, I am so confuse understanding. There are some examples in help, but I do not see any result with an equation, but a number.
    Out of curiosity, how much of your confusion is related to the Excel specific functions, and how much of the confusion is related to understanding regression/curve fitting in general? I find the Excel functions are much easier to understand if I understand the math behind the function.

    Here's a sample spreadsheet I created in Google docs (so I hope it converts back to Excel format). Note that I have created a column for each power of x that I want included in the known_x matrix (there are shortcuts for this when polynomials are involved, but this better illustrates exactly how LINEST() works with multiple x terms). I've used a 2nd order polynomial, it should be straightforward for you to add additional columns if desired.
    Attached Files Attached Files

+ 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