View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Toppers Toppers is offline
external usenet poster
 
Posts: 4,339
Default sorting a listbox in a user form

You will need to sort prior to adding to listbox. One option is to put your
data into a column in spreadsheet and use stndard Excel Sort. Then load
listbox from sorted data

e.g. Assume sorted data is in column A of Sheet1 starting row 1 (no header)

Private Sub UserForm_Initialize()
Dim lastrow as long, r as long
lastrow=Worksheets("Sheet1").cells(rows.count,"A") .end(xlup).row
For ri= 1 to Lastrow
userform1.listbox1.additem worksheets("Sheet1").cells(r,"A")
next i
End sub

Alternatively, you could use a internal sort routine e.g a Bubble Sort.

HTH

" wrote:

Dear All,
I need to sort a listbox after populating it in a userform via the
macro below. The list varies from time to time.

Private Sub UserForm_Initialize()
UserForm1.ListBox1.AddItem "Jane"
UserForm1.ListBox1.AddItem "Anna"
UserForm1.ListBox1.AddItem "Melinda"
End Sub

Appreciate some help on this.

Thank you in advance
Julian