+ Reply to Thread
Results 1 to 4 of 4

Sort a multi column listbox in userform

  1. #1
    Martin
    Guest

    Sort a multi column listbox in userform

    I have a 4 column listbox in a userform. I would like to sort the contents
    by pressing a command button above the column to be sorted. Can I do so and
    if so how do I do it.

    Thank You,
    --
    Martin

  2. #2
    Tom Ogilvy
    Guest

    Re: Sort a multi column listbox in userform

    How do you populate the listbox. If using the rowsource, try sorting the
    source data in the worksheet, then perhaps reassign the rowsource if
    necessary.

    --
    Regards,
    Tom Ogilvy

    "Martin" <[email protected]> wrote in message
    news:[email protected]...
    > I have a 4 column listbox in a userform. I would like to sort the

    contents
    > by pressing a command button above the column to be sorted. Can I do so

    and
    > if so how do I do it.
    >
    > Thank You,
    > --
    > Martin




  3. #3
    Martin
    Guest

    Re: Sort a multi column listbox in userform

    Tom,

    I have a datasource of about 2000 rows referencing insurance classes.
    Listbox3 consists of the major catagories of insurance ie: property,
    Liability, etc (about 20 catagories). Listbox4 populates from the datasource
    - listing the courses, credithours, type of instruction, and instructors,
    based on the criteria selected in listbox4.

    here is mycode for lisbox3_click
    Private Sub ListBox3_Click()
    Dim x As Integer, rw As Long, l As Integer
    Dim c As Range
    Dim mytest As String, mycol As Integer, mylookup As String
    ListBox4.Clear
    TextBox7.Text = ""
    TextBox8.Text = ""
    TextBox9.Text = ""
    ListBox4.ColumnCount = 4
    ListBox4.ColumnWidths = "190;40;47;14"

    mytest = ListBox3.Value
    mycol = ListBox3.ListIndex + 19
    mylookup = "1"
    With Worksheets("PC Analysis").Columns(mycol)
    Set c = .Find(mylookup, LookIn:=xlValues, MatchCase:=False)
    If Not c Is Nothing Then

    firstaddress = c.address
    Do
    temp = c.address
    l = Len(temp)
    rw = Right(temp, l - 3)
    ListBox4.AddItem (Worksheets("PC Analysis").Cells(rw, 10).Value)
    ListBox4.List(ListBox4.ListCount - 1, 1) = _
    Worksheets("PC Analysis").Cells(rw, 9).Value
    ListBox4.List(ListBox4.ListCount - 1, 2) = _
    Worksheets("PC Analysis").Cells(rw, 11).Value
    ListBox4.List(ListBox4.ListCount - 1, 3) = _
    Worksheets("PC Analysis").Cells(rw, 12).Value
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.address <> firstaddress
    End If
    End With
    End Sub

    mythought is to copy the contents of listbox4 into an array - sort it, based
    on column selected - ie button above columns clear lisbox4 and repopulate it
    with the sorted array. I think this would work but its pushing me.

    Thanks for your thoughts
    --
    Martin


    "Tom Ogilvy" wrote:

    > How do you populate the listbox. If using the rowsource, try sorting the
    > source data in the worksheet, then perhaps reassign the rowsource if
    > necessary.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "Martin" <[email protected]> wrote in message
    > news:[email protected]...
    > > I have a 4 column listbox in a userform. I would like to sort the

    > contents
    > > by pressing a command button above the column to be sorted. Can I do so

    > and
    > > if so how do I do it.
    > >
    > > Thank You,
    > > --
    > > Martin

    >
    >
    >


  4. #4
    Tom Ogilvy
    Guest

    Re: Sort a multi column listbox in userform

    Yes, that should work.

    I have posted code in the past to sort a multicolumn 2D array on a selected
    column. Search in Google Groups for this news group, and Ogilvy as author.

    --
    Regards,
    Tom Ogilvy

    "Martin" <[email protected]> wrote in message
    news:[email protected]...
    > Tom,
    >
    > I have a datasource of about 2000 rows referencing insurance classes.
    > Listbox3 consists of the major catagories of insurance ie: property,
    > Liability, etc (about 20 catagories). Listbox4 populates from the

    datasource
    > - listing the courses, credithours, type of instruction, and instructors,
    > based on the criteria selected in listbox4.
    >
    > here is mycode for lisbox3_click
    > Private Sub ListBox3_Click()
    > Dim x As Integer, rw As Long, l As Integer
    > Dim c As Range
    > Dim mytest As String, mycol As Integer, mylookup As String
    > ListBox4.Clear
    > TextBox7.Text = ""
    > TextBox8.Text = ""
    > TextBox9.Text = ""
    > ListBox4.ColumnCount = 4
    > ListBox4.ColumnWidths = "190;40;47;14"
    >
    > mytest = ListBox3.Value
    > mycol = ListBox3.ListIndex + 19
    > mylookup = "1"
    > With Worksheets("PC Analysis").Columns(mycol)
    > Set c = .Find(mylookup, LookIn:=xlValues, MatchCase:=False)
    > If Not c Is Nothing Then
    >
    > firstaddress = c.address
    > Do
    > temp = c.address
    > l = Len(temp)
    > rw = Right(temp, l - 3)
    > ListBox4.AddItem (Worksheets("PC Analysis").Cells(rw, 10).Value)
    > ListBox4.List(ListBox4.ListCount - 1, 1) = _
    > Worksheets("PC Analysis").Cells(rw, 9).Value
    > ListBox4.List(ListBox4.ListCount - 1, 2) = _
    > Worksheets("PC Analysis").Cells(rw, 11).Value
    > ListBox4.List(ListBox4.ListCount - 1, 3) = _
    > Worksheets("PC Analysis").Cells(rw, 12).Value
    > Set c = .FindNext(c)
    > Loop While Not c Is Nothing And c.address <> firstaddress
    > End If
    > End With
    > End Sub
    >
    > mythought is to copy the contents of listbox4 into an array - sort it,

    based
    > on column selected - ie button above columns clear lisbox4 and repopulate

    it
    > with the sorted array. I think this would work but its pushing me.
    >
    > Thanks for your thoughts
    > --
    > Martin
    >
    >
    > "Tom Ogilvy" wrote:
    >
    > > How do you populate the listbox. If using the rowsource, try sorting

    the
    > > source data in the worksheet, then perhaps reassign the rowsource if
    > > necessary.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > "Martin" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > I have a 4 column listbox in a userform. I would like to sort the

    > > contents
    > > > by pressing a command button above the column to be sorted. Can I do

    so
    > > and
    > > > if so how do I do it.
    > > >
    > > > Thank You,
    > > > --
    > > > Martin

    > >
    > >
    > >




+ 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