+ Reply to Thread
Results 1 to 17 of 17

Sort Numerical-With-Dash Values In Order

  1. #1
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Question Sort Numerical-With-Dash Values In Order

    Hey,

    I'm having issues with sorting numbers in the following format in numerical order:

    12345
    12345-1
    12345-2
    12345-3
    13456

    They're all in Column A, but whether I format the cells as text or numbers, trying to sort them in order results in the following:

    12345
    13456
    12345-1
    12345-2
    12345-3

    What must I do to make this work?
    Last edited by swordswinger710; 03-28-2012 at 10:56 AM.
    There is so much good in the worst of us,
    And so much bad in the best of us,
    That it hardly behooves any of us
    To talk about the rest of us.

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Sort Numerical-With-Dash Values In Order

    Possibly add another column with a formula like:

    =LEFT(A2,5) copied down

    Then sort by that column first, and the original second.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Valued Forum Contributor
    Join Date
    03-14-2012
    Location
    Arizona USA
    MS-Off Ver
    Excel 2000/2007
    Posts
    408

    Re: Sort Numerical-With-Dash Values In Order

    I would do a search/replace first.
    Change the "-" to "."

    Make sure that the cells are formatted as GENERAL.

    Do the sort(ascending).
    Then do the search/replace again.
    Change the "." to "-"

  4. #4
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Sort Numerical-With-Dash Values In Order

    NBVC, thank you, but I tried and failed to make your suggestion work - I don't quite know what you mean.

    Dennis7849, that's what I've been having to do, but it gets annoying after a while.

    Is there a code perhaps that would allow the sort function to work properly?

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Sort Numerical-With-Dash Values In Order

    I mean, if for example your sort data is in column A starting at A2, then in another column you enter: =LEFT(A2,5) and copy down.. this extract the left 5 chars (assuming the code is consistently 5 chars before the dash or without dash)... then you sort the whole table, and sort first by that formula column, then by the original column A...

  6. #6
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Sort Numerical-With-Dash Values In Order

    Okay, I see what you mean now - and that's cool, however, that's not going to work very well in my scenario..

    Would you mind taking a look at my sample worksheet? The little sort button in Cell A1 is the one that I need to be able to sort with.
    Attached Files Attached Files
    Last edited by swordswinger710; 03-28-2012 at 11:43 AM.

  7. #7
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Sort Numerical-With-Dash Values In Order

    It won't sort the way you want using just that.... Why won't the above method work in your scenario?

  8. #8
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Sort Numerical-With-Dash Values In Order

    Um, I just don't know how it would work. I don't want another column on that sheet. Could it be hidden? Could that sorting button somehow be linked to the second column and sort the A column accordingly?

  9. #9
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Sort Numerical-With-Dash Values In Order

    You can hide the helper column, but you can't use the button in A1 to sort accordingly... you can however use the SORT in the DATA tab of the ribbon to sort by the hidden column first, then by column A.... or you can record/create a macro with a button on the sheet to do that sort at the click of a "SORT" button....

  10. #10
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Sort Numerical-With-Dash Values In Order

    Ah, there we go then. I could just replace the little sort button thingy on Cell A1 with a sort button.

    Any idea how to create a button that could do this all in one click then? I'm trying to learn how to do the whole button thing, but am still in Grade 0, I'm afraid. I'd be most grateful if you had the time to walk me through it.

  11. #11
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Sort Numerical-With-Dash Values In Order

    I going offline for lunch, you can try recording a macro and assigning to a button.

    If it doesn't work for you, I will try something after lunch...

  12. #12
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Sort Numerical-With-Dash Values In Order

    I just got back from lunch myself. Okay, so is the second column necessary, or are we able to use the =LEFT(A*,5) formula right in the macro?

  13. #13
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Sort Numerical-With-Dash Values In Order

    Ok, so I am definitely not the VBA writing champion ... I lean toward formula solutions only.... but I recorded a macro that does what Dennis suggested (to eliminate the formula need) and sort.... and then I tweaked the macro a bit to git rid of extraneous stuff...

    Please Login or Register  to view this content.
    You could probably clean it up a bit more.. but it does the job, I think quickly.

  14. #14
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Sort Numerical-With-Dash Values In Order

    Haha, well you're definitely a VBA champion when compared to me.

    Nice, that seems to work. Do you know of a way we can have the block of cells deselect after clicking the button?

  15. #15
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Sort Numerical-With-Dash Values In Order

    You can add:

    Please Login or Register  to view this content.
    before the last application.screenupdate=True

  16. #16
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Sort Numerical-With-Dash Values In Order

    Beautiful. That does the trick! Thank you very much, NBVC. You've been most helpful.

  17. #17
    Registered User
    Join Date
    05-22-2015
    Location
    Portugal
    MS-Off Ver
    Windows7
    Posts
    1

    Re: Sort Numerical-With-Dash Values In Order

    How can i take the code

+ 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