+ Reply to Thread
Results 1 to 4 of 4

Sort by closest value to answer. Closest answer will appear at the top of the sorted list

  1. #1
    Registered User
    Join Date
    07-23-2019
    Location
    TW
    MS-Off Ver
    2016
    Posts
    12

    Sort by closest value to answer. Closest answer will appear at the top of the sorted list

    Hi, I am trying to automatically sort a column in excel using VBA.

    I want the closest value to my answer (my answer is found in sheet 2 but its hidden) to appear at the top of the sorted list. (The answer will be in a separate sheet, say sheet2, but my list will be in sheet1)

    for example, my answer is 4,000 and the closest answer is 3,998. I want the row where the answer is 3,998 to be shifted to the top of the list and likewise sort for all the other rows. So the whole list will be sorted based on which ones are the closest to the answer.

    This is the sorting code I found online, but idk how to include the part where I can sort according to my answer in another cell. Is that even possible?

    Please Login or Register  to view this content.
    Last edited by AAS44; 08-14-2019 at 11:30 AM.

  2. #2
    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,619

    Re: Sort by closest value to answer. Closest answer will appear at the top of the sorted l

    Not really, this code would do regular sort (in this particular retup - vertically, in ascending order.

    If you want to use such code you shall first compute the "distance" may be with the function Abs(value-your_answer).

    But indeed, the code will do the automatic sort (unfortunately not the way you want) always when you change (manually or by macro, but not as a result of formula) any cell in column B.

    If you do not feel confident about VBA it will be much easier to support you if you prepare a small sample file and attach it - see one of our "canned replies":
    Will you please attach a SMALL sample Excel workbook(s) (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If trere are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, etc. - please show them all or at least indicate in text) The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually).

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

    To attach an Excel file you have to do the following: Just before posting, scroll down and press Go Advanced button and then scroll down and press Manage Attachments link. Now follow the instructions at the top of that pop-up screen.
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    07-23-2019
    Location
    TW
    MS-Off Ver
    2016
    Posts
    12

    Re: Sort by closest value to answer. Closest answer will appear at the top of the sorted l

    Lets say i do =ABS(B2-Sheet2!$A$1). Where can i place this in my current code?

    Quote Originally Posted by Kaper View Post
    Not really, this code would do regular sort (in this particular retup - vertically, in ascending order.

    If you want to use such code you shall first compute the "distance" may be with the function Abs(value-your_answer).

    But indeed, the code will do the automatic sort (unfortunately not the way you want) always when you change (manually or by macro, but not as a result of formula) any cell in column B.

    If you do not feel confident about VBA it will be much easier to support you if you prepare a small sample file and attach it - see one of our "canned replies":
    Will you please attach a SMALL sample Excel workbook(s) (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If trere are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, etc. - please show them all or at least indicate in text) The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually).

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

    To attach an Excel file you have to do the following: Just before posting, scroll down and press Go Advanced button and then scroll down and press Manage Attachments link. Now follow the instructions at the top of that pop-up screen.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Sort by closest value to answer. Closest answer will appear at the top of the sorted l

    Quote Originally Posted by AAS44 View Post
    Lets say i do =ABS(B2-Sheet2!$A$1). Where can i place this in my current code?
    Without any context this means little to us.


    Please upload a workbook or a representative cut down copy, anonymised if necessary. It is always easier to advise if we can see your request in its context.

    Show a before and after situation with manually calculated results, explaining which information is data and which is results, and if it's not blindingly obvious how you have arrived at your results some explanatory notes as well.

    To upload a file click the Go Advanced button at the foot of your post, look underneath the post area for the Manage Attachments section and take it from there.
    Last edited by Richard Buttrey; 08-14-2019 at 01:32 PM.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ 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. Replies: 11
    Last Post: 06-05-2023, 01:36 AM
  2. Need to lookup closest lower and closest higher value
    By ashishkhurana93 in forum Excel General
    Replies: 7
    Last Post: 09-19-2018, 04:47 PM
  3. [SOLVED] Retrieving the Closest Larger / Closest Smaller Values from a List
    By ChipsSlave in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 03-31-2017, 08:38 AM
  4. [SOLVED] How would you Sort a list closest to a value automatically?
    By stockgoblin42 in forum Excel General
    Replies: 7
    Last Post: 03-23-2013, 08:33 AM
  5. [SOLVED] get 10 Closest Larger / Closest Smaller Values from a List
    By Auni in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-19-2013, 04:07 AM
  6. VBA Sort by Closest to TODAY
    By SailorGuy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-08-2008, 08:45 PM
  7. Replies: 0
    Last Post: 08-25-2005, 02:37 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