列表和计数从重复项中唯一

时间:2015-05-27 06:39:01

标签: excel vba excel-vba duplicates excel-2010

我有一个包含6列的表。第1列有重复:

ABC1
ABC2
ABC3
ABC4
ABC1
ABC1
ABC2
ABC4

我想要的是以下内容:

ABC1   3
ABC2   2
ABC3   1
ABC4   2

我想通过VBA实现上述目的,并将结果存储在2D数组中。如果无法通过VBA,我也愿意接受建议。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

Sub test()
    Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = vbTextCompare
    Dim Cl As Range, Data As Range, Key As Variant
    Set Data = Range(Cells(1, 1), Cells(Cells.Find("*", , , , xlByRows, xlPrevious).Row, 1))
    For Each Cl In Data
        If Not Dic.exists(Cl.Value) Then
            Dic.Add Cl.Value, 1
        Else
            Dic(Cl.Value) = CLng(Dic(Cl.Value)) + 1
        End If
    Next Cl
    For Each Key In Dic
        Debug.Print Key, Dic(Key)
    Next Key
    Set Dic = Nothing
End Sub

输出

enter image description here