tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,926
- Office Version
- 365
- 2019
- Platform
- Windows
This code sorts a collection:
It works if I test it as follows:
but fails if the "1" is not a string, ie this fails:
Can someone please tell me why is that?
Thanks
Code:
Public Function SortCollection(ByRef coll As Collection) As Collection
Dim i As Long, j As Long
Dim k As Variant
For i = 1 To coll.Count - 1
For j = i + 1 To coll.Count
If UCase(String:=coll.Item(i)) > UCase(String:=coll.Item(j)) Then
k = coll.Item(j)
With coll
.Remove j
.Add Item:=k, _
Key:=k, _
Before:=i
End With
End If
Next j
Next i
Set SortCollection = coll
End Function
It works if I test it as follows:
Code:
Sub Test()
Dim coll As Collection
Set coll = New Collection
coll.Add "a"
coll.Add "1"
Set coll = SortCollection(coll)
End Sub
but fails if the "1" is not a string, ie this fails:
Code:
Sub Test()
Dim coll As Collection
Set coll = New Collection
coll.Add "a"
coll.Add 1
Set coll = SortCollection(coll)
End Sub
Can someone please tell me why is that?
Thanks