+ Reply to Thread
Results 1 to 14 of 14

Sort listbox by number

  1. #1
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Sort listbox by number

    Hi

    I have to sort the Rows in a listbox, on an userform, by number in a columnA.



    Do any one have a solution for this?

    Regards

    Tony

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sort listbox by number

    There are ways to do it but it would depend on the data and how the listbox was filled.
    1. Listbox has 1 column or more?
    2. Where does data come from that fills the listbox?
    3. How was data added to listbox?
    4. Are there no duplicates and same number of rows in Column A as there are rows in the listbox?

    An attached short simple file would help us help you more easily and better. Click Go Advanced button in lower right of a reply and then the Manage Attachments hyperlink below the reply box to attach files.

  3. #3
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Re: Sort listbox by number

    Hi Kenneth.

    Listbox have 15 columns.

    Data is comming from af table in a sheet.

    The listbox is used to add new Rows in the table.

    The column A is an ID row so no dupliclates.

    I want the listbox to show the highest ID in the top, but in the table the highest ID should be in the bottom.

    Right now highest ID is in the bottom both plages, so I want to sort the listbox, before showing.

  4. #4
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Re: Sort listbox by number

    Upload finish
    Attached Files Attached Files

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sort listbox by number

    In a macro, I would make a Scratch WorkSheet and copy your data for the listbox. You can then sort it and use that as the RowSource.

    Rather than using CountA to get the last row in your dataset, I would use a column that must have data. That is usually the first column. e.g.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Re: Sort listbox by number

    Thank you for your time.

    I Can see your idea, to make extra a sheet,but is there no way to control the sort, direct before loading the boxlist? To avoid an extra sheet.

    As I see it, if I change the irow, it Will also change my final sheet (Ccp3) upside Down, I am trying to avoid this.

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sort listbox by number

    irow is simply the row number. CountA() counted an extra row because you had a smiley face character in that column. The 2nd commented line in #5 is the way that I would normally do that. In this case, it would be even worse as there would be several blank rows from end of data matrix to the row with that character. So, I used the down method. That has its own pitfalls. e.g. A blank row in dataset because of a blank value.

    A scratch worksheet could also be placed into a new scratch workbook. I usually prefer that method and then just close it without saving. e.g
    Please Login or Register  to view this content.
    It is a simple matter to sort that by viewing a recorded sort.

    RowSource shows the title/row's column headings which is about the only reason I use that sometimes.

    I normally like to use LIST in a Listbox to fill it. It gives me more power to add or remove list rows. Of course we trade-off the column header row feature of RowSource.

    Like the List method, a ArrayList would also need that column heading row. One way I handled that in the past was to use two listbox controls with a small one row listbox to hold the column headings. ArrayList methods can be sorted so that might be much faster than the scratch method.

    So, 2 array methods that I thought of can be sorted. One can get into weird sorting issues with numbers treated as strings with those methods and the standard range sort.

    Here is an example of the ArrayList sorting method.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Re: Sort listbox by number

    Hi Kenneth

    Thank you I Will try with the temp worksheet.

  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sort listbox by number

    I made an ArrayList method but the sort was by string and not numeric by column A.

    The scratch sheet method worked as I understood #1.

    In your Userform code change this part below. You can delete the 2 commented lines if it works as needed.
    Please Login or Register  to view this content.
    In a Module, first Sub is just for a quick test.
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 03-14-2021 at 01:00 PM.

  10. #10
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Re: Sort listbox by number

    That is a great help. Thank you

  11. #11
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Re: Sort listbox by number

    Hi Kenneth I have tryed to use your code, but it gives me failure.

    Not enough memory, if I try to use other funtions like EDIT

    Do you know a way out of this ?

  12. #12
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sort listbox by number

    How many rows of data are there?

    Try a manual copy of table to another blank workbook.

    DoEvents after the copy line might help.

  13. #13
    Registered User
    Join Date
    08-04-2020
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2016
    Posts
    56

    Re: Sort listbox by number

    There are 4 or 5 rows

  14. #14
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Sort listbox by number

    You must have something else going on. I used your example file to test.

+ 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. A sort of listbox
    By dodde in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2020, 07:23 AM
  2. Hide a row in a Listbox, Or Sort a Listbox
    By jcastrejon1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-03-2019, 02:19 AM
  3. [SOLVED] Need to alphabetically sort listbox.
    By IMM Tech in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-24-2018, 12:59 PM
  4. [SOLVED] Sort Listbox in Userform
    By s2jrchoi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-02-2013, 02:13 PM
  5. Sort listbox chronologically
    By olivierpbeland in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-23-2013, 02:20 PM
  6. How to sort a listbox alphabetically?...
    By 10121730 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-01-2012, 03:03 AM
  7. Sort a listbox
    By FRIEL in forum Excel Programming / VBA / Macros
    Replies: 33
    Last Post: 05-25-2011, 04:07 AM

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