+ Reply to Thread
Results 1 to 10 of 10

VBA code to calculate the distance between point and the regression line

  1. #1
    Registered User
    Join Date
    05-16-2016
    Location
    Earth
    MS-Off Ver
    7
    Posts
    4

    VBA code to calculate the distance between point and the regression line

    Hello guys. I made a function that calculates the distances between the points (of a X,Y table) and the regression line, while you need only the X and Y ranges/tables and of course their values. Here's what it looks like:

    =ABS((ABS(SLOPE(J23:J32,I23:I32))*I23-J23+INTERCEPT(J23:J32,I23:I32))/SQRT((SLOPE(J23:J32,I23:I32)^(2)+(1))))

    Where J23:J32 & I23:I32 happens to be the Y and X value ranges/tables respectively. And I23 is the X and the J23 is the Y.

    So I'm basicaly trying to make a code in VBA for this function so I can use this regularly because of my job but unfortunately, I have no idea. I'd like the function to ask only for the range of the Known X's and Y's (just like the slope function) and the x and y values.
    Can anyone help please?
    Last edited by Lalikos; 05-16-2016 at 06:44 PM.

  2. #2
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: VBA code to calculate the distance between point and the regression line

    Hi Lalikos,
    Try this:
    Please Login or Register  to view this content.
    Let me know if you have any issues/questions
    Regards,
    Stephen

    If you feel someone has helped you please thank them and click on the star on their post to add reputation.
    Please ensure that you use code tags where relevant, and mark solved threads as solved.
    Most of all please be respectful and understanding of others.

  3. #3
    Registered User
    Join Date
    05-16-2016
    Location
    Earth
    MS-Off Ver
    7
    Posts
    4

    Re: VBA code to calculate the distance between point and the regression line

    Quote Originally Posted by spitfireblue View Post
    Hi Lalikos,
    Try this:
    Please Login or Register  to view this content.
    Let me know if you have any issues/questions
    Thank you so much man, that worked for me!! And now I have two (and the last i promise) more questions.

    1) How can I lock the X & Y range through the code (just like what the "$" symblom does) because I want to use the AutoFill option, and just drag it down for each empty cell, but the range everytime moves one cell down. For example if a table of X & Y's is the (A:1,B:10), in the rest of the cells which they lie underneath, AutoFill will every time change the range (addind one cell) e.g.(A:2,B:11) to (A:3,B:12) to (A:4,B13) and so on..

    And..

    2) How can I save this function in order to use it again in the future?
    Last edited by Lalikos; 05-17-2016 at 06:31 AM.

  4. #4
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: VBA code to calculate the distance between point and the regression line

    Hi,
    Not sure I completely understand your first question, but have a look at this attachment and let me know if this is what you are after: Example.xlsm

    For your second question, you can set up the function as an addin that opens every time you open Excel. Here is what you need to do:
    1. Open a new workbook and copy the function into a new module
    2. Save the workbook in C:\Users\(your username)\AppData\Roaming\Microsoft\AddIns as an xlsm (this becomes your backup)
    3. Save the workbook again, this time as an .xlam file in the same folder
    4. Open Excel and go to Excel Options > Add-Ins > Go and then find your new Add In and tick the box, then select OK

    Let me know how you go and if you have any further questions.

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

    Re: VBA code to calculate the distance between point and the regression line

    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    05-16-2016
    Location
    Earth
    MS-Off Ver
    7
    Posts
    4

    Re: VBA code to calculate the distance between point and the regression line

    Quote Originally Posted by spitfireblue View Post
    Hi,
    Not sure I completely understand your first question, but have a look at this attachment and let me know if this is what you are after: Attachment 461313
    Yes that's exactly what I mean! At your attach sir, at the CalcDist function cell(s) (D column) you have placed the $ dollar symbol to refer these values.
    I personaly didn't place the $ symbol and used the autocomplete, taking the results that I'm posting.
    So is it possible to modify the code so I can use this function only to the first cell of the column and then just drag it down (using the auto-Fill) taking the right results?

    Screenshot_1.jpg Screenshot_2.jpg
    Last edited by Lalikos; 05-18-2016 at 03:37 AM.

  7. #7
    Registered User
    Join Date
    05-16-2016
    Location
    Earth
    MS-Off Ver
    7
    Posts
    4

    Re: VBA code to calculate the distance between point and the regression line

    Quote Originally Posted by shg View Post
    Hello. Well I don't know if this is illegal or something, or if these two forums have (?) any hostility between them, but the reason I posted on both forums, was that I would have greater chance of getting 1) a reply and 2) the solution I needed. I'm really sorry if I wasn't allowed to do this.

  8. #8
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: VBA code to calculate the distance between point and the regression line

    Quote Originally Posted by Lalikos View Post
    So is it possible to modify the code so I can use this function only to the first cell of the column and then just drag it down (using the auto-Fill) taking the right results?
    Not really, you just need to add the $ signs in to make it an absolute reference.
    Have a look at this website that explains how relative and absolute references work: http://www.gcflearnfree.org/excel2013/15.2

  9. #9
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: VBA code to calculate the distance between point and the regression line

    Quote Originally Posted by Lalikos View Post
    Hello. Well I don't know if this is illegal or something, or if these two forums have (?) any hostility between them, but the reason I posted on both forums, was that I would have greater chance of getting 1) a reply and 2) the solution I needed. I'm really sorry if I wasn't allowed to do this.
    It is a good idea to read the rules before starting a thread. As per rule #8 you can cross-post but you should provide a link, which is what shg has done for you.

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

    Re: VBA code to calculate the distance between point and the regression line

    ... deleted ...
    Last edited by shg; 05-18-2016 at 11:37 AM.

+ 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. calculate maximum distance from a line that intersects a curve
    By DRAGOS ROTARU in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 02-03-2016, 06:01 AM
  2. Slope, distance, expeculating next point on a line
    By kaiknux in forum Excel General
    Replies: 1
    Last Post: 01-26-2015, 07:57 AM
  3. Calculate distance, offset and height difference from a line to a point
    By Shwgroove in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-16-2014, 05:40 AM
  4. Replies: 4
    Last Post: 10-29-2013, 01:07 PM
  5. shortest distance between a point and a line
    By stockgoblin42 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-04-2013, 02:18 PM
  6. Perpendicular distance of a point from linear regression line?
    By Winny in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 09-24-2005, 08:05 PM
  7. Replies: 6
    Last Post: 09-21-2005, 07:05 PM

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