繁体   English   中英

汇总B列中的值。仅当A列中有相同值时

[英]Sum values that are in column B. Only when there are same value in column A

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

例子转这个:

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

  • 100 ....... 10.00

  • 100 ........ 15.00

  • 200 ........ 30.00

  • 300 ....... 15.50

变成这个:

  • .. A ............ B
  • 100 ...... 25.00

  • 200 ....... 30.00

  • 300 ....... 15.50

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM