成语大全网 - 汉语词典 - EXCEL如何用VBA数据字典:除去重复后汇总

EXCEL如何用VBA数据字典:除去重复后汇总

以下为代码及注释:

Sub main()

Set dic = CreateObject("scripting.dictionary") '定义词典

arr = Range("A1:C500") '假设最大行数为500,将A1至C500区域放入数组

For i = 1 To UBound(arr) '从1到数组最大行数循环

If arr(i, 1) <> "" And arr(i, 1) <> "装置" Then '提出空格和标题行

dic(arr(i, 1)) = dic(arr(i, 1)) + arr(i, 3) '用字典去除重复,并且进行数量累加

End If

Next i

[e2].Resize(dic.Count, 1) = Application.Transpose(dic.keys) '将字典的keys(即A列不重复的值)转置并放在E列

[f2].Resize(dic.Count, 1) = Application.Transpose(dic.items) '将字典的kItems(即累加结果)转置并放在E列

End Sub