Consulting

Results 1 to 4 of 4

Thread: How to sort Listbox with Multiple columns

  1. #1

    How to sort Listbox with Multiple columns

    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


  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •