我正在寻找使用VB.net在excel工作表中获得唯一值的总和。 我正在使用收藏 到目前为止,我的代码使我获得了不同的值,但是我对事物的计数感到困惑。 我觉得我已经接近了,但是缺少了一些东西……
我的数据可能如下:
Apple
Apple
Peach
Cherry
我正在寻找结果为
Apple 2
Peach 1
Cherry 1
这是我的位置
MySub:
Dim c, r As Range
Dim i As Integer
Dim dc As New Collection
Dim s As String
For Each c In r
dc.Add(c.Value, c.Value)
Next c
For i = 1 To dc.Count
s = dc.Item(i)
Next i
这产生了我独特的值列表,但是我没有看到如何获取这些值的SUM。 感谢您的指导。
答案 0 :(得分:0)
假设这确实是VB.Net,则可以这样使用Dictionary(Of String, Integer)
:
Dim counts As New Dictionary(Of String, Integer)
For Each c In r
If Not counts.ContainsKey(c.Value) Then
counts.Add(c.Value, 0)
End If
counts.Item(c.Value) = counts.Item(c.Value) + 1
Next c
For Each pair In counts
Debug.Print(pair.Key & " " & pair.Value)
Next