在列中获取10个最常见的名字 [英] Getting the 10 Top most frequent names in a column

查看:34
本文介绍了在列中获取10个最常见的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been struggling to come up with a program capable of extracting the 10 most frequent names appearing in a column and storing them into an array for further use.

推荐答案

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

Gather the column's values into an array to speed processing. Transfer to a dictionary's keys with the frequency as each key's item. The worksheet's Large can easily find the 10th largest frequency. Remove anything that has a lower frequency.

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

这篇关于在列中获取10个最常见的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆