MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2002, 04:56 PM   #1
tonyBruckner
New Member
 
Join Date: Apr 2002
Posts: 21
Default

How do I get the addItems in a listBox to be laid out alphabetically?

I'm filling out the listBox using a For statement that moves through a large assembly of names (Doe,John). The assembly is a seating chart and consequently cannot be put in order on the spreadsheet. Names that pass certain conditions in the loop are added to the listBox.

How and where in this system can I alphabetize the listBox?
tonyBruckner is offline   Reply With Quote
Old Apr 19th, 2002, 05:45 PM   #2
XL Pro
Board Regular
 
XL Pro's Avatar
 
Join Date: Apr 2002
Location: =VLOOKUP("XL Pro",Earth,1,FALSE)
Posts: 205
Default

They're a several ways to accomplish this. One is to port the list to a worksheet, sort it there, then reimport the items into the Listbox.
Another way is to read the items into a string array, use the below "bubble sort" routine, and re-add the items to the listbox.
Code:
Sub BubbleSort(List() As String)
    '   Sorts the List Array in ascending order
    Dim First As Integer, Last As Integer
    Dim i As Integer, J As Integer
    Dim Temp
    First = LBound(List)
    Last = UBound(List)
    For i = First To Last - 1
        For J = i + 1 To Last
            If List(i) > List(J) Then
                Temp = List(J)
                List(J) = List(i)
                List(i) = Temp
            End If
        Next J
    Next i
End Sub
For performance issues, collections can be used, but I don't have that code handy.
__________________
Not as Lean
Not as Mean
But still a Marine
Semper Fi
XL Pro is offline   Reply With Quote
Old Apr 19th, 2002, 06:21 PM   #3
smozgur
BatCoder
 
Join Date: Feb 2002
Location: Turkey
Posts: 764
Default

Use this codes in a sample userform with a listbox. Then you can apply it for your userform.

Private Sub UserForm_Activate()
Dim i As Integer
Dim a

'Here we are creating the listbox with data
'You already have this step, we are doing this to have a sample listbox
a = Split("XX,A,RR,LL", ",")
For i = 0 To UBound(a)
ListBox1.AddItem a(i)
Next i

'Now sorting is begining
ReDim oldData(ListBox1.ListCount) As String
Dim sortedData() As String

'Set an array values with the listbox values to use in bubble sort
For i = 0 To ListBox1.ListCount - 1
oldData(i) = ListBox1.List(i)
Next i

'Call the sort routine
sortedData = BubbleSort(oldData)

'Refreshing listbox with sorted data
ListBox1.Clear
For i = 0 To UBound(sortedData)
If Len(Trim(sortedData(i))) > 0 Then ListBox1.AddItem sortedData(i)
Next i

End Sub


Public Function BubbleSort(Strings() As String) As String()
Dim a As Long, b As Long, c As Long, d As Long
Dim e As Integer, f As Integer, g As Integer
Dim i As String, j As String
Dim m() As String, n() As String

e = 1
n = Strings
Do While e <> -1

For a = 0 To UBound(Strings) - 1
i = n(a)
j = n(a + 1)
f = StrComp(i, j)
If f <= 0 Then
n(a) = i
n(a + 1) = j
Else
n(a) = j
n(a + 1) = i
g = 1
End If
Next a
If g = 1 Then
e = 1
Else
e = -1
End If
g = 0
Loop
BubbleSort = n
End Function


regards
smozgur is offline   Reply With Quote
Old Apr 19th, 2002, 11:44 PM   #4
nisht
Board Regular
 
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
Default

Sorting is most discussed topic in world of programming .. this the sentence written by
'mark harris' whom book i refer for my difficulties.

'bubble sort method' as described here is the method mostly used... i presume.
there other methods also for sorting..

like,,
insertion sorts..
ripple sort
shell sort
recursive method of sorting...

well gone to far..

download my file nos..8 'sheet name'

it is example file exactly what you need
nishith desai
http://www.pexcel.com
nisht is offline   Reply With Quote
Reply

Bookmarks

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 On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 11:52 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes