Results 1 to 19 of 19

Thread: [RESOLVED] Sort Listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Resolved [RESOLVED] Sort Listbox

    I have this code that populates a Listbox with the sheets in the workbook.
    I need a way to sort the items in the Listbox.


    Code:
            Dim listbox1 As ListBox
                For Each ws In ThisWorkbook.Sheets
    
                If Not (ws.Name = "Data" Or ws.Name = "Index") _
                    Then Sheets("Index").listbox1.AddItem ws.Name
                If ws.Index > 5 Then ws.Visible = False
                    Next

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Sort Listbox

    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    Hey Sid! I couldn't get it to work.

    I have this in the workbook module
    Code:
            Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    '            Application.ScreenUpdating = False
                If Sheets("Index").ToggleButton1.Value = False Then
                    Dim ws As Worksheet
                    Set ws = Sh
                If ws.Index > 5 Then
                    ws.Move , Sheets("Index")
                    Sheets("Index").ListBox1.Clear
                    For Each ws In ThisWorkbook.Sheets
                If Not (ws.Name = "Data" Or ws.Name = "Index") _
                    Then Sheets("Index").ListBox1.AddItem ws.Name
                If ws.Index > 5 Then ws.Visible = False
                    Next
                End If
                End If
                SortListBox  'Hangs up here. (this is how you call another macro right?)
                
            End Sub
    and I put this in a Module

    Code:
     Sub SortListBox(oLb As MSForms.ListBox)
        Dim vaItems As Variant
        Dim i As Long, j As Long
        Dim vTemp As Variant
        
        ‘Put the items in a variant array
        vaItems = oLb.List
        
        For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
            For j = i + 1 To UBound(vaItems, 1)
                If vaItems(i, 0) > vaItems(j, 0) Then
                    vTemp = vaItems(i, 0)
                    vaItems(i, 0) = vaItems(j, 0)
                    vaItems(j, 0) = vTemp
                End If
            Next j
        Next i
        
        ‘Clear the listbox
        oLb.Clear
        
        ‘Add the sorted array back to the listbox
        For i = LBound(vaItems, 1) To UBound(vaItems, 1)
            oLb.AddItem vaItems(i, 0)
        Next i
        
    End Sub

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sort Listbox

    Look at what you are doing
    Code:
    SortListBox  'Hangs up here. (this is how you call another macro right?)
    Now, look at what the sub routine is expecting
    Code:
    Sub SortListBox(oLb As MSForms.ListBox)
    Can you see why not only isn't it working, but you are getting an error (which you neglected to mention BTW)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    Hack, the error is Compile Error: Argument not Optional
    I tried putting the whole thing in SortListBox(oLb As MSForms.ListBox), but it comes back with an error immediately. Compile Error: Expected: List Seperator or ) with the "As" highlighted Blue.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Sort Listbox

    sortlistbox listbox1
    the required argument is the listbox to sort
    if you had multiple listboxes you can sort all by passing the listbox to sort
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sort Listbox

    I knew what the error was before you posted it. You were calling the sub without passing it the parameter it required.

    oLb As MSForms.ListBox is a parameter that must be passed. In this case, as westconn1 points out, it is the name of a listbox.

    Anytime you use a sub or function that has listed parameters, or arguments, you HAVE to pass something to it in order to fulfill that requirement.

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Sort Listbox

    What you are doing is:
    1. Clear the ListBox
    2. Add everything you have into the ListBox
    3. Copy the list of the ListBox to an array
    4. Sort the array
    5. Clear the ListBox again
    6. Add the sorted array back to the ListBox

    That is a crazy process! Why not sort while adding items into the ListBox?

    With not too many items, you can use a simple linear search before adding an item in a right order place:
    Code:
            With Sheets("Index").ListBox1
                .Clear
                For Each ws In ThisWorkbook.Sheets
                    If ws.Name <> "Data" And ws.Name <> "Index" Then
                        For i = 0 To .ListCount - 1
                            If ws.Name < .List(i) Then Exit For
                        Next
                        .AddItem ws.Name, i
                    End If
                    If ws.Index > 5 Then ws.Visible = False
                Next
            End With
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    Hack, West, I'm can't seem to grasp what your saying.
    I have tried:
    Sub SortListBox(oLb As MSForms.ListBox1)
    Call:
    SortListBox Listbox1
    Nothing works, what am I doing wrong?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    Anhn, I tried your code, but it errors.

    Code:
            Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    '            Application.ScreenUpdating = False
                If Sheets("Index").ToggleButton1.Value = False Then
                    Dim ws As Worksheet
                    Set ws = Sh
                If ws.Index > 5 Then
                    ws.Move , Sheets("Index")
                        With Sheets("Index").ListBox1.Clear
                For Each ws In ThisWorkbook.Sheets
                    If ws.Name <> "Data" And ws.Name <> "Index" Then
                        For i = 0 To .ListCount - 1    'Errors here Runtime Error 424: Object required 
                                                                'Seems to error on sheet42 Varient Empty.
                            If ws.Name < .List(i) Then Exit For
                        Next
                        .AddItem ws.Name, i
                    End If
                    If ws.Index > 5 Then ws.Visible = False
                Next
            End With
                End If
                End If
            End Sub

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Sort Listbox

    My code is:
    Code:
            With Sheets("Index").ListBox1
                .Clear
    but you wrongly combined 2 lines into one:
    Code:
    With Sheets("Index").ListBox1.Clear
    You should learn how to ident your code properly:
    Code:
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        'Application.ScreenUpdating = False
        If Sheets("Index").ToggleButton1.Value = False Then
            Dim ws As Worksheet
            If Sh.Index > 5 Then
                Sh.Move After:=Sheets("Index")
                With Sheets("Index").ListBox1
                    .Clear
                    For Each ws In ThisWorkbook.Sheets
                        If ws.Name <> "Data" And ws.Name <> "Index" Then
                            For i = 0 To .ListCount - 1
                                If ws.Name < .List(i) Then Exit For
                            Next
                            .AddItem ws.Name, i
                        End If
                        If ws.Index > 5 Then ws.Visible = False
                    Next
                End With
            End If
        End If
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    Anhn,

    Ohhh! I remember doing that. I thought it was a mistake. my bad! ;-)

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Sort Listbox

    This should work just fine
    Code:
    SortListBox Listbox1
    What happens when you run it?

  14. #14
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Sort Listbox

    If use SortListBox(), the call should be:
    Code:
        SortListBox Sheets("Index").ListBox1
    ListBox1 need to be qualified.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    Anhn, I have both ways working now, but on your code I can't seem to figure out how to populate the listbox initially on Workbook Open. I have this:

    Code:
            Private Sub Workbook_Open()
    
                Sheets("Data").Visible = False
    
                For Each ws In ThisWorkbook.Sheets
                    If ws.Name <> "Data" And ws.Name <> "Index" Then
                        For i = 0 To .ListCount - 1      ' Compile Error: invalid or unqualified reference
                            If ws.Name < .List(i) Then Exit For
                        Next
                        .AddItem ws.Name, i
                    End If
                    If ws.Index > 5 Then ws.Visible = False
                Next
                    Sheets("Index").Activate
                With Sheets("Index").ListBox1
                    .Height = 92.4
                    .Width = 184
                    .Top = 79.2
                    .Left = 249
                End With
    
            End Sub
    Last edited by tome10; May 30th, 2010 at 07:45 PM.

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Sort Listbox

    Do you know how to use With...End With?
    Code:
    Private Sub Workbook_Open()
        Sheets("Data").Visible = False
        Sheets("Index").Activate
        With Sheets("Index").ListBox1
            .Height = 92.4
            .Width = 184
            .Top = 79.2
            .Left = 249
            For Each ws In ThisWorkbook.Sheets
                If ws.Name <> "Data" And ws.Name <> "Index" Then
                    For i = 0 To .ListCount - 1
                        If ws.Name < .List(i) Then Exit For
                    Next
                    .AddItem ws.Name, i
                End If
                If ws.Index > 5 Then ws.Visible = False
            Next
        End With
    End Sub
    Without using
    With Sheets("Index").ListBox1
    the code becomes:
    Code:
    Private Sub Workbook_Open()
        Sheets("Data").Visible = False
        Sheets("Index").Activate
        Sheets("Index").ListBox1.Height = 92.4
        Sheets("Index").ListBox1.Width = 184
        Sheets("Index").ListBox1.Top = 79.2
        Sheets("Index").ListBox1.Left = 249
        For Each ws In ThisWorkbook.Sheets
            If ws.Name <> "Data" And ws.Name <> "Index" Then
                For i = 0 To Sheets("Index").ListBox1.ListCount - 1
                    If ws.Name < Sheets("Index").ListBox1.List(i) Then Exit For
                Next
                Sheets("Index").ListBox1.AddItem ws.Name, i
            End If
            If ws.Index > 5 Then ws.Visible = False
        Next
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    When I use both of the above codes, the Listbox is not formed correctly (too wide, too tall) on workbook open. When I go back in and manually run the code a 2nd time it is formed correctly. ideas?

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: Sort Listbox

    For some reason the Listbox gets sized correctly when the sizing code is after the populating code. It is working now. Thanks for all your help.


    Code:
    Private Sub Workbook_Open()
        Sheets("Data").Visible = False
        Sheets("Index").Activate
    
        For Each ws In ThisWorkbook.Sheets
            If ws.Name <> "Data" And ws.Name <> "Index" Then
                For i = 0 To Sheets("Index").ListBox1.ListCount - 1
                    If ws.Name < Sheets("Index").ListBox1.List(i) Then Exit For
                Next
                Sheets("Index").ListBox1.AddItem ws.Name, i
            End If
            If ws.Index > 5 Then ws.Visible = False
        Next
        Sheets("Index").ListBox1.Height = 95
        Sheets("Index").ListBox1.Width = 182
        Sheets("Index").ListBox1.Top = 79.2
        Sheets("Index").ListBox1.Left = 249
    End Sub

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2009
    Posts
    448

    Re: [RESOLVED] Sort Listbox

    Can you guys help me out on my other project? Please! ;-)

    http://www.vbforums.com/showthread.php?t=616347

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
  •  



Click Here to Expand Forum to Full Width