Sorting a MultiColumn ListBox

This example sorts the items in a two-column ListBox. Here’s what the unsorted example ListBox looks like

SortMulti

The code behind the Sort button looks like this

And the sorted ListBox

SortMulti2

13 thoughts on “Sorting a MultiColumn ListBox

  1. Hi,
    I was wondering if you could help with a simple problem. I am not very used to VBA functions. I was wondering if you could help with a simple VBA routine that allows me to sort the data in a column in descending order. I have 3 columns with data and want to sort the column 1 in descending order with corresponding data in column 2 and 3 matching the corresponding column 1 data.

    I am able to record macros and do it, but I would need a function that actually creates a user defined function in the dropdown function list in the spreadhsheet. Putting the recorded macros in a function did not help. I guess its very simple for expert user. I would need a user defined function for my problem to be solved.

    Would appreciate your help.
    Thanks a lot

    Sudeep

  2. How can one still use the bubble sort? The worst sorting algorithm invented by man!

  3. The much maligned bubble sort is unsophisticated, rough and crude.
    It is also often times much easier to implement in certain situations.

    I saw comparisons of algorithms for data sets where the quick sort was
    1000 times faster. To the modern microprocessor the difference often is
    1/10000 of a second verses 1/10th of a second ! (oh how impatient we have become :)

    I appreciate a good kludge but i am unappreciative of “sophisticated”
    algorithms that simply do not work. Remember the mantra of programming
    that it is 10 times more difficult to debug than it is to write a program.
    So not only is simpler often the better route, a program can become so complex that
    it transcends the ability of the writer to debug. So lets not just yet throw the bubble
    sort under the bus :)

    Gil

  4. @Ahmed: Change the comparison line to look at the second element of the array instead of the first

    Everything else stays the same.

  5. Hello,

    Thanks for your bubble sort code. It has been a great help as I am a newbie to VBA code.

    I have used your code to populate a list box with 5 columns. The first column (col 0) in the listbox is numerical with numbers between 1 to 40. Unfortunately, the sort on numbers produces something like 1,10,11, 12, 2, 21, 22 etc. Is there anyway I could change your code so that the code could sort the numbers correctly i.e 1,2,3,4 etc.

    I do not know very much about VBA code, so any help you could give would be greatly appreciated.

    Thank you

  6. how do populate 2 columns that are independent values. ie you can select fish in the second column only. When I populate it puts scroll bar on the list box and does not fill the second column

  7. ‘Same code for 3 columns including reverse sorting :
    ‘My columns are File Name, File Type and File Path, so the names of the buttons

Leave a Reply

Your email address will not be published. Required fields are marked *