+ Reply to Thread
Results 1 to 11 of 11

Use VBA codes to Fnd the closst point and record the row

  1. #1
    Registered User
    Join Date
    09-15-2009
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Question Use VBA codes to Fnd the closst point and record the row

    i have 10 points inserted in colume A, row 2-11(exclude row 1 due to title)
    and 10 points(ie.random order 1-10) inserted in colume B, row 2-11
    I have a point with value 5.8 in cell D4
    and a point with value5.8 in cell D5
    i wanna find the point in colume A which is closest to value in D4
    and find the point in colume B which is cloest to value in D5

    The answer should be the value 6,
    but what i want to find is what row is the answer in, and show in the cell D6
    so what should appear in D6 is 7, because the value 6 is in the 7th row.

    can someone point me a direction of how to do this?
    btw, i understand simple VBA code, im using direct cell reference here just to keep it simple.
    To whoever cares about my question and spend time on it~
    Thanks you! i am really grateful~~
    Attached Files Attached Files
    Last edited by kyod; 09-17-2009 at 02:19 PM. Reason: Solved

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Use VBA code to Fnd the closest point and record the row

    It looks like are looking for two different values, the row in column A that contains the value that is closest to the value in cell D4, and the row in column B that contains the value that is closest to the value in cell D5.
    Since you are looking for two different values, you probably want two cells to display those two values. You could use D6 for column A and D7 for column B, how does that sound?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Use VBA code to Fnd the closest point and record the row

    If that is acceptable to you, use this code:

    Please Login or Register  to view this content.
    If there are any issues or questions, please ask.

  4. #4
    Registered User
    Join Date
    09-15-2009
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Use VBA code to Fnd the closest point and record the row

    First, thanks for helping~
    The thing is, i really want is to test the whole col, instead of row 2 - 11,(the 1-10 value is just example), because i want it to be able to apply to different sheet, which have similar structure but contains different value in COL A and COL B, so the answer should be display as
    ActiveSheet.Cells(7,4)=CurrentBestRow

    so that it may be use in different cell,
    Thus what COL A and COL B represent is like X,Y points of a map,
    and i have this certain point of X,Y in cell D4,D5
    while i have the points in COL A and COL B forming a shape(ie Hexagon) around this point
    so the code to find a point X,Y in ColA(X) and ColB(Y), which closest to this given point in cells D4,D5, thus record down the row of the cloest point are in,
    Hopefully you understand what i mean, anyway, thank you again for spending time on it,
    and if possible, can you figure out a code that works out the furthest point as well?
    and record down in cell D8 or something for all activesheets.

  5. #5
    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: Use VBA code to Fnd the closest point and record the row

    Posting a workbook with an example and expected results would be a good thing.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Use VBA code to Fnd the closest point and record the row

    It would be easier to help if you included all your requests in your original post. If you wanted it for an entire column instead of rows 2-11 and you don't know how to modify the code to do that yourself, plz include that as a request in your first post.

    Its hard to understand what exactly you are looking for, but I've modified the code to go through all rows in columns 1 & 2, and also put the worst rows in cells D9 and D10. If you are doing this for multiple worksheets, use a simple for each loop. If you need help with that type for in the vb editor, put the cursor inside of the word and hit F1 to bring up the help.

    Please Login or Register  to view this content.
    Again, any more questions, please ask.

  7. #7
    Registered User
    Join Date
    09-15-2009
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Use VBA code to Fnd the closest point and record the row

    sry for the late reply,
    the fact is that i may have misunderstood the question,


    Thank you so much for helping~
    Last edited by kyod; 09-16-2009 at 10:27 PM.

  8. #8
    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: Use VBA code to Fnd the closest point and record the row

    is it possible that you leave me a email so i can send the specific sheet to you
    Please don't. The point of a forum is to help all members learn. You can post your workbook here.

    If I were you, I would remove my email address from your last post.

  9. #9
    Registered User
    Join Date
    09-15-2009
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Use VBA code to Fnd the closest point and record the row

    Quote Originally Posted by shg View Post
    Please don't. The point of a forum is to help all members learn. You can post your workbook here.

    If I were you, I would remove my email address from your last post.
    Soz, i have attach the sheet at the top, cloest point and furest point is what im trying to find

  10. #10
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Use VBA codes to Fnd the closst point and record the row

    This posted example is significantly different from your first request. It deals with each row as a point, so you can't just find the closest and farthest number in each column. You have to use the distance formula, which is d=((x1-x2)^2 + (y1-y2)^2)^(1/2).

    Here is the updated code:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-15-2009
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Use VBA codes to Fnd the closst point and record the row

    Thx a million man!
    and sry about the first request, because i misunderstood the question as well,
    the updated code is exactly wat i needed.
    im really thankful that i got your help, because VBA is very new to me, and im struggling to understand it.
    Again, thank you and this forum~!

+ 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