PDA

View Full Version : How to sort Listbox with Multiple columns



vmjamshad
10-30-2017, 08:13 AM
Dear All,

My Data has 4 columns with the following headings:
Heading 1,Heading 2,Heading 3 and Heading 4.

I have created a Listbox using the below code. In the user from i have 2 Option Buttons which indicates 2 columns. I want to sort my list box based on the selection. Example: if the select the 'serial number' option box, it should filter based on the first column value (Heading 1)and if i choose 'job' it should filter based on the third column value (Heading 3)

Can anybody help me to find a code for that.

Please see below the code for Listbox.

Private Sub UserForm_Initialize()

With ListBox1
.ColumnCount = 4
.ColumnWidths = "130;30;30;130"
End With

LstRow = Cells(Rows.Count, 1).End(xlUp).Row

For a = 0 To LstRow - 2
b = a + 2
ListBox1.AddItem
ListBox1.list(a, 0) = Cells(b, 4)
ListBox1.list(a, 1) = Cells(b, 1)
ListBox1.list(a, 2) = Cells(b, 3)
ListBox1.list(a, 3) = Cells(b, 2)

Next a

End Sub

Kenneth Hobs
10-30-2017, 09:59 AM
Since you are showing a different order than in the workbook, copy your data to a scratch sheet. Then sort to suit. Set the rowsource to that range. Include the column headers which rowsource will show as well.

For static properties like columncount and columnwidths, add the values manually.

YasserKhalil
10-30-2017, 10:21 AM
Have a look at this link
http://www.ozgrid.com/forum/showthread.php?t=71509

Kenneth Hobs
10-30-2017, 02:35 PM
If you want to see an example of my method, see attached file.

It also has the macro from post #3's link. If that method interests you, I would suggest copying my userform and then try to use it there.