vba 字典查重计数
时间: 2023-12-29 11:26:23 浏览: 30
以下是使用VBA字典进行查重计数的示例:
vba
Sub CountDuplicates()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim rng As Range
Set rng = RangeA1:A10") '将范围更改为你要查重的范围
Dim cell As Range For Each cell In rng
If dict.exists(cell.Value) Then
dict(cell.Value) = dict(cell.Value) + 1
Else
dict.Add cell.Value, 1
End If
Next cell
Dim key As Variant
For Each key In dict.keys
If dict(key) > 1 Then
Debug.Print key & "出现了" & dict(key) & "次"
End If
Next key
End Sub
```
这段代码首先创建了一个字典对象,然后遍历指定范围内的单元格,将单元格的值作为字典的键。如果字典中已经存在该键,则将对应的值加1;否则,将该键添加到字典中,并将对应的值设置为1。最后,遍历字典中的键,如果对应的值大于1,则表示该值重复出现了多次。