我正在尝试编写一种通用排序,该排序将对键上的VBA集合进行排序。我以排序here为起点。
如果集合包含对象,则排序不起作用(如下所示)。如何修改代码以获取用于排序的密钥?
我能够找到的信息得到的是键的集合,而不是单个项目的键(例如,这里的VBA collection: list of keys)。
下面显示的当前代码在Debug.print行上给出“对象不支持此属性或方法”。
Public Function sort(ByRef col As VBA.collection)
For i = 1 To col.Count - 1
For j = i + 1 To col.Count
Debug.Print ("Key: " & col(i))
If col(i) > col(j) Then
'store the lesser item
temp = col(j)
'remove the lesser item
col.Remove j
're-add the lesser item before the greater item
col.Add temp, temp, i
End If
Next j
Next i
End Function
Sub test()
Dim col As collection
Set col = New collection
Dim pi As ProgramIncrement
' val
Set pi = New ProgramIncrement
pi.name = "foo3"
col.Add pi, pi.name
' val
Set pi = New ProgramIncrement
pi.name = "foo0"
col.Add pi, pi.name
' val
Set pi = New ProgramIncrement
pi.name = "foo2"
col.Add pi, pi.name
' val
Set pi = New ProgramIncrement
pi.name = "foo1"
col.Add pi, pi.name
' sort
sort col
Debug.Print ("Done.")
End Sub
' ---------------------------------------------------------------
'
' Class to represent Program Increment
'
' ---------------------------------------------------------------
Public name As String
Public sprints As New collection