|
-
May 27th, 2010, 11:54 PM
#1
Thread Starter
Hyperactive Member
[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
-
May 28th, 2010, 02:10 AM
#2
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
-
May 28th, 2010, 10:44 AM
#3
Thread Starter
Hyperactive Member
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
-
May 28th, 2010, 11:24 AM
#4
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)
-
May 28th, 2010, 11:55 AM
#5
Thread Starter
Hyperactive Member
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.
-
May 29th, 2010, 03:00 AM
#6
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
-
May 29th, 2010, 07:15 AM
#7
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.
-
May 29th, 2010, 07:41 AM
#8
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
-
May 29th, 2010, 02:56 PM
#9
Thread Starter
Hyperactive Member
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?
-
May 29th, 2010, 03:03 PM
#10
Thread Starter
Hyperactive Member
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
-
May 29th, 2010, 06:33 PM
#11
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
-
May 29th, 2010, 11:04 PM
#12
Thread Starter
Hyperactive Member
Re: Sort Listbox
Anhn,
Ohhh! I remember doing that. I thought it was a mistake. my bad! ;-)
-
May 30th, 2010, 07:25 AM
#13
Re: Sort Listbox
This should work just fine
Code:
SortListBox Listbox1
What happens when you run it?
-
May 30th, 2010, 07:32 AM
#14
Re: Sort Listbox
If use SortListBox(), the call should be:
Code:
SortListBox Sheets("Index").ListBox1
ListBox1 need to be qualified.
-
May 30th, 2010, 07:41 PM
#15
Thread Starter
Hyperactive Member
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.
-
May 31st, 2010, 12:24 AM
#16
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
-
May 31st, 2010, 12:40 PM
#17
Thread Starter
Hyperactive Member
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?
-
May 31st, 2010, 08:58 PM
#18
Thread Starter
Hyperactive Member
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
-
Jun 2nd, 2010, 10:42 PM
#19
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|