Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Sorting Listbox Values Alphabetically ??


Reply
 
Thread Tools Display Modes
  #1  
Old 02-19-2007, 03:38 AM
coreytroy coreytroy is offline
Newcomer
 
Join Date: Apr 2006
Posts: 6
Talking Sorting Listbox Values Alphabetically ??


Is it possible to code a listbox to populate in an Alphabetical order ?
How ?

Corey....
Reply With Quote
  #2  
Old 02-19-2007, 06:32 AM
spacey123 spacey123 is offline
Centurion
 
Join Date: Dec 2003
Location: UK
Posts: 181
Default

I don't think it's possible to sort it directly, but one way could be to get the listbox contents into an array/worksheet, sort it, then repopulate the listbox (?).
Reply With Quote
  #3  
Old 02-19-2007, 12:23 PM
Michael_I Michael_I is offline
Contributor

* Expert *
 
Join Date: Dec 2001
Posts: 725
Default

Hello!

I took the following code from the link below and adapted it for your problem:
http://www.dailydoseofexcel.com/arch...ing-listboxes/

The list should display A - D even though the values were added in a different order.

Code:
Private Sub UserForm_Initialize() Dim vaItems As Variant Dim i As Long, j As Long Dim vTemp As Variant Me.ListBox1.AddItem "B" Me.ListBox1.AddItem "A" Me.ListBox1.AddItem "D" Me.ListBox1.AddItem "C" 'Put the items in a variant array vaItems = Me.ListBox1.List 'Steal code from John Walkenbach’s Excel Power Programming 'with VBA to sort the array 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 Me.ListBox1.Clear 'Add the sorted array back to the listbox For i = LBound(vaItems, 1) To UBound(vaItems, 1) Me.ListBox1.AddItem vaItems(i, 0) Next i End Sub

Let us know if this helps!

~Mike

p.s. Please change the ListBox name if it is not "ListBox1". Thanks!
Reply With Quote
  #4  
Old 02-19-2007, 12:56 PM
Timbo's Avatar
Timbo Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

As shown above, the Bubble Sort technique is popular - with variations:
http://www.freevbcode.com/ShowCode.asp?ID=6770

Also, the MS Forms Listbox 'List' property accepts an array, removing the need to iterate the source array using the 'AddItem' method
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->