如何找到集合中的前 n 个值?

How do I find the top n values in a collection?

因此,我有一列数据已分为几个(非连续的)集合,我想突出显示每个集合中最重要的值。到目前为止,我已经设法突出显示每个集合中的每个值,但我不知道如何只找到集合的最大值,更不用说可变数量的最高值了。

这是我目前的情况:

Sub Test()

Dim i As Integer
Dim t As Variant
Dim T1 As New Collection
Dim T2 As New Collection
Dim T3 As New Collection


    'Sort into collections
    For i = 2 To 195
        If Cells(i, 14) = "" Then
                Rows(i).EntireRow.Hidden = True
        ElseIf Cells(i, 14) < 10000 Then
                T1.Add Cells(i, 16)
        ElseIf Cells(i, 14) > 100000 Then
                T3.Add Cells(i, 16)
        Else
                T2.Add Cells(i, 16)
        End If
    Next i

    'colour cells
    For Each t In T1
            t.Interior.Color = RGB(204, 236, 255)
    Next t

     For Each t In T2
            t.Interior.Color = RGB(204, 204, 255)
    Next t

     For Each t In T3
            t.Interior.Color = RGB(204, 153, 255)
    Next t

End Sub

我想用

之类的东西替换“彩色单元格”部分
For Each t in T1 
    If t > (nth largest value in T1) Then 
        t.Interior.Color = RGB(whatever)
    End If 
Next t 

我想知道调用 LARGE 函数并为 k 使用变量是否可行,但我担心它需要搜索的单元格的非连续性在该函数中不起作用。

如果能帮我解决这个问题,我将不胜感激 <3

您可以使用 SortedList 对象,而不是 Collection,并将 P 列值作为其键,将 P 列单元格作为其项,以便它们按键自动排序:

Option Explicit

Sub Test()
    Dim i As Long

    Dim T1 As Object
    Dim T2 As Object
    Dim T3 As Object

    Set T1 = CreateObject("System.Collections.SortedList") ' set a sorted list object
    Set T2 = CreateObject("System.Collections.SortedList") ' set a sorted list object
    Set T3 = CreateObject("System.Collections.SortedList") ' set a sorted list object

    For i = 2 To 195
        If Cells(i, 14) = "" Then
                Rows(i).EntireRow.Hidden = True
        ElseIf Cells(i, 14) < 10000 Then
                T1.Add Cells(i, 16).Value, Cells(i, 16) ' add an element to T1 sorted list with current column P value as key and current column P cell as item
        ElseIf Cells(i, 14) > 100000 Then
                T3.Add Cells(i, 16).Value, Cells(i, 16) ' add an element to T1 sorted list with current column P value as key and current column P cell as item
        Else
                T2.Add Cells(i, 16).Value, Cells(i, 16) ' add an element to T1 sorted list with current column P value as key and current column P cell as item
        End If
    Next i

    ColourIt T1, 4, RGB(204, 236, 255)
    ColourIt T2, 4, RGB(204, 204, 255)
    ColourIt T3, 4, RGB(204, 153, 255)
End Sub


Sub ColourIt(T As Object, ByVal maxElementsNumber As Long, color As Long)
    Dim j As Long, lastElementIndex As Long

    With T ' reference passed object
        If maxElementsNumber <= .Count Then lastElementIndex = .Count - maxElementsNumber 'set last element index to be colored according to sorted list actual items number

        For j = .Count - 1 To lastElementIndex Step -1 ' loop through sorted list items backwards to start from the highest key down to the lowest
            .GetByIndex(j).Interior.color = color ' color current sorted list item
        Next
    End With

End Sub