尝试对集合进行排序时键入不匹配

时间:2018-11-17 14:59:18

标签: excel vba

在对已经包含对象的集合进行排序时,我遇到类型不匹配的情况,我无法终生。

有人可以解释为什么会发生错误吗?下面是我用来对集合进行排序的代码。

Private Sub SortItems(combinedItems As Collection)
    Dim counter As Integer, _
        counter2 As Integer, _
        temp As Variant, _
        tempColleciton As New Collection, _
        currentSortItem As SortItem, _
        nextSortItem As SortItem, _
        sortDirection As SortType

        sortDirection = SortOrder

    For counter = 1 To combinedItems.Count - 1
        For counter2 = counter + 1 To combinedItems.Count
            Set currentSortItem = combinedItems(counter)
            Set nextSortItem = combinedItems(counter2)
            If currentSortItem.Key > nextSortItem.Key Then
                combinedItems.Remove counter2

                If sortDirection = Ascending Then
                    combinedItems.Add nextSortItem, nextSortItem, counter 'error occurs here if Ascending
                Else
                    combinedItems.Add nextSortItem, nextSortItem, After:=counter 'error occurs here if Decending
                End If

            End If
        Next counter2
    Next counter

End Sub

1 个答案:

答案 0 :(得分:0)

您是否要使用对象作为集合中的键?

combinedItems.Add nextSortItem, nextSortItem, counter

尝试使用:

combinedItems.Add nextSortItem, nextSortItem.Key, counter