excel - 获取列中最常用的 10 个名称

标签 excel vba

我一直在努力想出一个能够提取出现在列中的 10 个最常见名称并将它们存储到数组中以供进一步使用的程序。

最佳答案

将列的值收集到一个数组中以加快处理速度。以频率作为每个键的项目转移到字典的键。工作表的大可以轻松找到第 10 大频率。删除任何频率较低的东西。

Option Explicit

Sub gfdrew()
    Dim i As Long, j As Long, arr As Variant, k As Variant, dict As Object

    Set dict = CreateObject("scripting.dictionary")

    With Worksheets("sheet6")
        arr = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp)).Value2
    End With

    For i = LBound(arr, 1) To UBound(arr, 1)
        dict.Item(arr(i, 1)) = dict.Item(arr(i, 1)) + 1
    Next i

    j = Application.Large(dict.items, Application.Min(10, dict.Count))

    For Each k In dict.keys
        If dict.Item(k) < j Then dict.Remove (k)
    Next k

    arr = dict.keys

    For i = LBound(arr) To UBound(arr)
        Debug.Print arr(i)
    Next i
End Sub

关于excel - 获取列中最常用的 10 个名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52214927/

相关文章:

excel - 如何使用 "pagination"单击网络中的某个页面?

python - 如何将 CSV 文件中的行存储到 Python 中并使用 HTML 打印数据

python - Excel - 查找前一行和各种范围大小的最大值

excel - 使用 While 循环获取特定列中的行句柄

vba - WinHTTP VBA 后续请求不能使用之前的登录凭据?

vba - Excel VBA For 循环不起作用,不断出现错误消息

excel - 如何将这些代码块截断为一个嵌套的 for 循环?

python - Pandas .txt文件到.csv文件缺少行

vba - 尽管公式是正确的,但在单元格中写入公式会出现错误

Excel VBA : Run a Formula as long as there is a value in Another Column