Sorting ListBox Items alphabetically

This is the simple code to sort ListBox items alphabetically. Alternatively, you can download the file and test run it for yourself. Just pass the listbox object as function parameter.
Option Explicit Sub SortListBox(lst As MSForms.ListBox) Dim arrItems As Variant Dim arrTemp As Variant Dim intOuter As Long Dim intInner As Long arrItems = lst.List For intOuter = LBound(arrItems, 1) To UBound(arrItems, 1) For intInner = intOuter + 1 To UBound(arrItems, 1) If arrItems(intOuter, 0) > arrItems(intInner, 0) Then arrTemp = arrItems(intOuter, 0) arrItems(intOuter, 0) = arrItems(intInner, 0) arrItems(intInner, 0) = arrTemp End If Next intInner Next intOuter 'Clear the listbox lst.Clear 'Add the sorted array back to the listbox For intOuter = LBound(arrItems, 1) To UBound(arrItems, 1) lst.AddItem arrItems(intOuter, 0) Next intOuter End Sub Sub ExecuteFunction() Call SortListBox(Sheet1.ListBox1) End Sub

| Attachment | Size |
|---|---|
| SortListBox.xls | 35.5 KB |
»
- Vishesh's blog
- Login or register to post comments
- 49853 reads

Recent comments
6 years 36 weeks ago
7 years 22 weeks ago
7 years 34 weeks ago
7 years 37 weeks ago
7 years 38 weeks ago
7 years 43 weeks ago
7 years 51 weeks ago
8 years 1 day ago
8 years 1 day ago
8 years 2 days ago