Excel 仅当A列中的值相同时,才对B列中的值求和
列A有参考号。列B具有各自的值。如果A列中有相同的参考号,如何从B列中求和它们的值并对它们进行排序 将此示例转换为:Excel 仅当A列中的值相同时,才对B列中的值求和,excel,Excel,列A有参考号。列B具有各自的值。如果A列中有相同的参考号,如何从B列中求和它们的值并对它们进行排序 将此示例转换为: .A..B 100。。。。。。。10点 100………15.00 200………30.00 300……15.50 进入这个: ..A..B 100……25.00 200………30.00 300……15.50 在A上排序,然后选择A:B,数据>大纲-幸运的话,默认设置将适合。如果要删除详细信息,请在顶部复制并粘贴特殊值,筛选以选择列A不包含tot,删除标签以外的可见内容,然后取消
- .A..B
- 100。。。。。。。10点
- 100………15.00
- 200………30.00
- 300……15.50
- ..A..B
- 100……25.00
- 200………30.00
- 300……15.50
tot
,删除标签以外的可见内容,然后取消筛选。如果不需要,则删除最后一行,如果不需要,则将Total
替换为nothing。如果需要,请取消分组。这应该是什么?SQL?至少详细告诉我们你想做什么。@RaphaëlAlthaus这不是我想要的。如果A列中有相同的值,我只想对B列中的一些行值求和。在我的例子中,只需要求B1和B2的和,因为A1和A2中有相同的值。
Sub RunSummary()
Dim rngTable As Range
Dim rngCursor As Range
Dim strKey As String
Dim dblValue As Double
Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary
Set rngTable = Range("C3:D6") ' assuming your table is in Range(C3:D6)
'insert value into dictionary
For Each rngCursor In rngTable.Rows
strKey = rngCursor.Cells(1, 1).Value
dblValue = rngCursor.Cells(1, 2).Value
'if the key already exists, add the current value to the existing value
If dict.Exists(strKey) Then
dict.Item(strKey) = dict.Item(strKey) + dblValue
Else
dict.Add strKey, dblValue
End If
Next
'printing the dictionary at cell C9
Dim i As Integer
Dim rngOutput As Range
Set rngOutput = Range("C9")
Dim key As Variant
For Each key In dict.Keys
rngOutput.Offset(i, 0).Value = key
rngOutput.Offset(i, 1).Value = dict(key)
i = i + 1
Next
End Sub