仅在列B中的总和值。仅当列A中的值相同时

时间:2014-07-02 07:54:58

标签: excel

A 有参考编号。列 B 具有各自的值。如果A列中有相同的参考编号,如何对B列中的值求和并对它们进行排序?

示例转为:

  • A ................ B

  • 100 ....... 10.00

  • 100 ........ 15.00

  • 200 ........ 30.00

  • 300 ....... 15.50

进入:

  • ..的 A ............的
  • 100 ...... 25.00

  • 200 ....... 30.00

  • 300 ....... 15.50

2 个答案:

答案 0 :(得分:0)

按A排序然后选择A:B,DATA>大纲 - Subtotal幸运的是,默认设置适合。如果要删除详细信息,请在顶部复制并粘贴特殊值,过滤以选择ColumnA不包含tot,删除除标签以外的可见和未过滤。如果不需要,请删除最后一行,如果不需要,请将Total替换为空。如果需要,可以取消组合。

答案 1 :(得分:0)

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