+ Reply to Thread
Results 1 to 16 of 16

Excel Split a Curve and Fit a trendline using VBA - - URGENT

  1. #1
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Hello all,
    Iam a civil student and for a lab test I need to solve about 450 excel sheets for my project.
    The spreadsheet has X and Y data and curve is plotted. I need to split the curves into 3 parts based on slope change.
    I can do it manually but I need an excel Macro which does this work. Could any one help me.

    A sample is attached.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,455

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    see macro "test"
    divide your data into 3 equal parts, calculate each slope and intercept and add to chart.
    now create yourself a loop to check if a certain point should stay in its section or go to a previous or next section.
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,873

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    As the answer above is based on worksheet formulas - I wonder if better definition of "good fit" shouldn't be based on RSQ function. See for instance output of such macro:
    Please Login or Register  to view this content.
    of course you shall do plotting (not just msgbox :-P ) after calculating a slope and intercept (but it is nicely covered by bslav example).

    And my extra comment -> this is just crunching numbers
    My personal taste after "optical trend recognition" would be 4 not 3 parts:
    first (blows) 0-3, second 4-19, third 21-34 and fourth 39-91

    After checking with some extra data, I am rather towards concept of having common points on borders of our subranges, so the loops look like:

    Please Login or Register  to view this content.
    Last edited by Kaper; 02-03-2014 at 05:43 AM.
    Best Regards,

    Kaper

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT


  5. #5
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Thanks Sir. This macro is good for only this data.
    But Sir, If I have to run it on other sheets, the chart is being plotted with the range A2:A24 for 1st series always.How to make it dynamic for other charts too I want to split when the Rsquare value is less than specified. Your help is of great deed.

    Thanks for your concern.

  6. #6
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,455

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    see attachment

    in that file, there is only 1 chart, so you should make a loop for your 450 charts.
    Do that with the macro "FirstValues"
    there you see in red
    SplitCurve MyX, MyY, 3
    With that instruction, you split the x and y values in 3 sections and afterwarts adapt the chart.
    So you have to specify that 450 times.
    If there is a certain structure in your data, that 'll be easy, otherwise ...


    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    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: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    I think this is too slow to be practical, but it's entertaining to watch.
    Attached Files Attached Files
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    This works fine... i'll try to refine it further..

    But is there a way which dynamically divides the chart into some number of parts depending on the slopes.
    say sample 1 has 3 parts. 1st series of 5 data points, second series of 10 data points and third series of 15 data points.
    say for sample 2, This may differ.

  9. #9
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Quote Originally Posted by shg View Post
    I think this is too slow to be practical, but it's entertaining to watch.
    Thanks Shg. This sheet works very good. But for my project I want to learn more. Can i know more about Piece wise Linear approximation

  10. #10
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Quote Originally Posted by bsalv View Post
    see attachment

    in that file, there is only 1 chart, so you should make a loop for your 450 charts.
    Do that with the macro "FirstValues"
    there you see in red
    SplitCurve MyX, MyY, 3
    With that instruction, you split the x and y values in 3 sections and afterwarts adapt the chart.
    So you have to specify that 450 times.
    If there is a certain structure in your data, that 'll be easy, otherwise ...


    Please Login or Register  to view this content.

    This works fine... i'll try to refine it further..

    But is there a way which dynamically divides the chart into some number of parts depending on the slopes.
    say sample 1 has 3 parts. 1st series of 5 data points, second series of 10 data points and third series of 15 data points.
    say for sample 2, This may differ.

  11. #11
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Quote Originally Posted by shg View Post
    I think this is too slow to be practical, but it's entertaining to watch.
    Its a huge VBA code, that I cannot understand. But just have a look at it, when the data is changed. I have changed the data and i gave 3 segments for ir to run.

    Please find the attachment.
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,455

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    what do you want exactly ?
    With my "SplitCurve MyX, MyY, 3", the macro choose the 3 segments to best fit the curve. That was 1-4,4-24,24-36 (see sheet2, with the regressionfunction).
    Do you want now to say that it's not 1-4,4-24,24-36 but for example 1-5,5-15,15-30 ? So the solution 'll be less ideal !
    Or do you want some points to be deleted before calculation ?

  13. #13
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Quote Originally Posted by bsalv View Post
    what do you want exactly ?
    With my "SplitCurve MyX, MyY, 3", the macro choose the 3 segments to best fit the curve. That was 1-4,4-24,24-36 (see sheet2, with the regressionfunction).
    Do you want now to say that it's not 1-4,4-24,24-36 but for example 1-5,5-15,15-30 ? So the solution 'll be less ideal !
    Or do you want some points to be deleted before calculation ?
    ooops...seems iam causing some irritation here. But sir, check out this attachment. If my next set of data is less than this size (36 rows), there is a run time error, which when tried to debug has error on this line.
    Please Login or Register  to view this content.
    .

    Sorry for troubling. but iam learning more and more
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,455

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Irritation no, confusion !
    I suppose that you're not familiar with tables, so your data is no longer a table but normal data.
    In the macro "FirstValues", you specify now the ranges of your X and Y-values in a way which is easier to understand.
    To know the name of the graph, you run the macro "GraphNames" and you get the name of the graph and the address of the topleft cell of that graph (topleft cell = the cell with the topleft corner of your graph in it).
    There is an important remark, your values in the columns A and C in your example are "strings" !!!
    If you looked at your graph, that had a very funny shape, so that was already a first indication.
    If you modified the format of the columns A and C to a number with 2 digits after the comma (format "0.00" or perhaps "0,00", depending on your international settings), you should have seen that they remained unchanged. 2nd indication that it were strings. (look how my number look like in columns A and C)

    Now everything is ready to run the macro "FirstValues".
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    09-06-2011
    Location
    Chennai,India
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    Quote Originally Posted by bsalv View Post
    Irritation no, confusion !
    I suppose that you're not familiar with tables, so your data is no longer a table but normal data.
    In the macro "FirstValues", you specify now the ranges of your X and Y-values in a way which is easier to understand.
    To know the name of the graph, you run the macro "GraphNames" and you get the name of the graph and the address of the topleft cell of that graph (topleft cell = the cell with the topleft corner of your graph in it).
    There is an important remark, your values in the columns A and C in your example are "strings" !!!
    If you looked at your graph, that had a very funny shape, so that was already a first indication.
    If you modified the format of the columns A and C to a number with 2 digits after the comma (format "0.00" or perhaps "0,00", depending on your international settings), you should have seen that they remained unchanged. 2nd indication that it were strings. (look how my number look like in columns A and C)

    Now everything is ready to run the macro "FirstValues".
    Now understood and is working fine for most of the data. The problem is with the number of data points for different samples.
    As the present chart is based on some N1 values of (x,y) for sample 1 as in the sheet. Assuming N2 for sample 2, N3 for sample 3............
    The chart works fine if N(i+1)>N(1) but it shows error if N(i+1)<N1.

  16. #16
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,455

    Re: Excel Split a Curve and Fit a trendline using VBA - - URGENT

    post me an example, that's easier to explain, because it depends a little bit on your layout.
    Are your X-values all the same range or not ?
    Is it all in the same graph ?

+ 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] Excel 2010 Trendline With Data labels or number on forward forecast trendline
    By camelight in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 10-15-2015, 08:35 AM
  2. Excel-Split a curve and make trendlines using VBA
    By develaavi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2014, 12:36 PM
  3. Need urgent help with interpolation on trendline
    By ZeroGrav in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 05-20-2008, 06:49 AM
  4. [SOLVED] Trendline - split one into two
    By Richard in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 10-27-2005, 12:05 AM
  5. how do you split the scale (x-axis) on a curve graph?
    By valeval1000 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-11-2005, 10:06 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