TheSwamp
Code Red => VB(A) => Topic started by: Humbertogo on September 05, 2007, 07:50:30 AM
-
Is there a way to sort comboboxes alphabetically
-
author unknown
Public Function CBsort(CB As ComboBox)
' Sort the Combobox
Dim CBvar As Variant
For I = 0 To CB.ListCount - 2
If CB.List(I) > CB.List(I + 1) Then
CBvar = CB.List(I)
CB.List(I) = CB.List(I + 1)
CB.List(I + 1) = CBvar
I = -1
End If
Next I
End Function
-
Thanks for the code.
only this take a long time to sort a list with in 90.00000 items :-(
-
Sort the information PRIOR to adding it to the combobox. What's your code look like? Can you post a snippet?
-
it might be easier/faster to read the items into an array, sort the array then add the array to the combobox
-
Thanks for the code.
only this take a long time to sort a list with in 90.00000 items :-(
How many ??
-
http://vbnet.mvps.org/index.html?code/sort/qsoverview.htm
QuickSort may be the one, but it will take a few seconds for sure
-
Thanks for the code.
only this take a long time to sort a list with in 90.00000 items :-(
How many ??
exactly !
why would you want/need to add 9000000 items to a combo box ??????
-
The simple solution is to set the Sorted property to True in the IDE at designtime.
At runtime a ComboBox Sorted property is read only, thus you need to set it prior to running the code.
-
The simple solution is to set the Sorted property to True in the IDE at designtime.
At runtime a ComboBox Sorted property is read only, thus you need to set it prior to running the code.
But a ComboBox in VBA doesn't have an option for SORTED. (unless it's called something else??)
-
Hmmmm .. I have been working in VB recently .. hadn't considered the fact the properties would be different in VB and VBA ...
let me see what I can come up with
-
thanks
'fill MyArray
For i = 1 To UBound(ComboBox1.List)
MyArray(i) = ComboBox1.List(i)
Next i
QuickSort MyArray, LBound(MyArray), UBound(MyArray)
ComboBox1.Clear
'use sorted array to refill ComboBox
For i = 1 To UBound(MyArray)
If MyArray(i) <> "" Then _
ComboBox1.AddItem MyArray(i)
Next i
Private Sub QuickSort(strArray() As String, intBottom As Integer, intTop As Integer)
Dim strPivot As String, strTemp As String
Dim intBottomTemp As Integer, intTopTemp As Integer
intBottomTemp = intBottom
intTopTemp = intTop
strPivot = strArray((intBottom + intTop) \ 2)
While (intBottomTemp <= intTopTemp)
While (strArray(intBottomTemp) < strPivot And intBottomTemp < intTop)
intBottomTemp = intBottomTemp + 1
Wend
While (strPivot < strArray(intTopTemp) And intTopTemp > intBottom)
intTopTemp = intTopTemp - 1
Wend
If intBottomTemp < intTopTemp Then
strTemp = strArray(intBottomTemp)
strArray(intBottomTemp) = strArray(intTopTemp)
strArray(intTopTemp) = strTemp
End If
If intBottomTemp <= intTopTemp Then
intBottomTemp = intBottomTemp + 1
intTopTemp = intTopTemp - 1
End If
Wend
'the function calls itself until everything is in good order
If (intBottom < intTopTemp) Then QuickSort strArray, intBottom, intTopTemp
If (intBottomTemp < intTop) Then QuickSort strArray, intBottomTemp, intTop
End Sub
-
It appears you are reading the list into the combobox, then reading the combobox list into an array.
Why not make it quicker by not reading into the combobox to begin with.. just read the items directly into the array
-
Here's one that I've used quite a bit...
Sub BubbleSort(arr As Variant, Optional descending As Boolean, Optional numEls As Variant)
' Bubble Sort an array of any type
' Author: The VB2TheMax Team
' BubbleSort is especially convenient with small arrays (1,000
' items or fewer) or with arrays that are already almost sorted
'
' NUMELS is the index of the last item to be sorted, and is
' useful if the array is only partially filled.
'
' Works with any kind of array, except UDTs and fixed-length
' strings, and including objects if your are sorting on their
' default property. String are sorted in case-sensitive mode.
'
' You can write faster procedures if you modify the first two lines
' to account for a specific data type, eg.
' Sub BubbleSortS(arr() As Single, Optional descending As Boolean, Optional numEls As Variant)
' Dim value As Single
Dim Value As Variant
Dim Index As Long
Dim firstItem As Long
Dim indexLimit As Long, lastSwap As Long
' account for optional arguments
If IsMissing(numEls) Then numEls = UBound(arr)
firstItem = LBound(arr)
lastSwap = numEls
Do
indexLimit = lastSwap - 1
lastSwap = 0
For Index = firstItem To indexLimit
Value = arr(Index)
If (Value > arr(Index + 1)) Xor descending Then
' if the items are not in order, swap them
arr(Index) = arr(Index + 1)
arr(Index + 1) = Value
lastSwap = Index
End If
Next
Loop While lastSwap
End Sub