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

时间:2018-10-25 21:00:11

标签: excel vba excel-2016

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

这是我到目前为止所拥有的:

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

1 个答案:

答案 0 :(得分:2)

您可以使用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