Hi Tom,
Thanks! Your code does what you promissed; the sorting is fine. But when I
try to merge it with my code (I need to get unique items) I either get an
error or there is no more sorting...
This was my original code:
With Me.ComboBox2
.Clear ' clear the listbox content
MyUniqueList3 = UniqueItemList(Range("genres"), True)
For g = 1 To UBound(MyUniqueList3)
.AddItem MyUniqueList3(g)
Next g
.ListIndex = 0 ' select the first item
End With
I would say this should work...
Dim MyUniqueList3 As Variant, g As Long
Dim i As Long, j As Long, v As Variant
Dim tmp As Variant
With Me.ComboBox2
.Clear ' clear the listbox content
' this is a function that give unique items, "genres" is my named
range
MyUniqueList3 = UniqueItemList(Range("genres"), True)
v = MyUniqueList3
For i = 1 To UBound(v, 1) - 1
For j = i + 1 To UBound(v, 1)
If v(i, 1) > v(j, 1) Then
tmp = v(i, 1)
v(i, 1) = v(j, 1)
v(j, 1) = tmp
End If
Next
Next
.List = v
End With
But then I get an error code # 9 "Subscript out of range" (or something
alike - I use a Dutch version)
With the following code there are no errors, but no sorting too...
With Me.ComboBox2
.Clear ' clear the listbox content
MyUniqueList3 = UniqueItemList(Range("genres"), True)
For g = 1 To UBound(MyUniqueList3)
.AddItem MyUniqueList3(g)
Next g
' sorting by Tom Ogilvy
Dim i As Long, j As Long, v As Variant
Dim tmp As Variant
v = .List
For i = 1 To UBound(v, 1) - 1
For j = i + 1 To UBound(v, 1)
If v(i, 1) > v(j, 1) Then
tmp = v(i, 1)
v(i, 1) = v(j, 1)
v(j, 1) = tmp
End If
Next
Next
.List = v
.ListIndex = 0 ' select the first item
End With
I must be overlooking something...
Regards,
Bart