简体   繁体   English

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

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

Column A has reference numbers. A具有参考编号。 Column B has their respective values. B列具有各自的值。 If there are same reference numbers in column A how to sum their values from column B and sort them ? 如果A列中的参考标号相同,如何将B列中的值求和并对其排序?

Example turn this : 例子转这个:

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

  • 100 ....... 10.00 100 ....... 10.00

  • 100 ........15.00 100 ........ 15.00

  • 200 ........30.00 200 ........ 30.00

  • 300 .......15.50 300 ....... 15.50

Into this : 变成这个:

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

  • 200.......30.00 200 ....... 30.00

  • 300.......15.50 300 ....... 15.50

Sort on A then select A:B, DATA > Outline - Subtotal and with luck the default settings will suit. 在A上排序,然后选择A:B,数据>大纲- 小计 ,幸运的是,默认设置适用。 If you want to remove the detail then Copy and Paste Special Values over the top, filter to select for ColumnA does not contain tot , delete visible other than labels and unfilter. 如果要删除详细信息,则在顶部复制并粘贴特殊值,为ColumnA选择的过滤器不包含tot ,删除除标签以外的可见标签,然后取消过滤。 Delete last row if not required and replace Total with nothing, if not required. 如果不需要,请删除最后一行,如果不需要,则将Total替换为空。 Ungroup if desired. 根据需要取消分组。

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.

相关问题 仅当列A和列B中的所有相邻值都相同时,才可以将列C中的值求和吗? - Is it possible to sum values in column C into column D, ONLY when all adjacent values in columns A and B are the same? 将列A中所有具有相同值的列A中的所有值相加 - Sum all values from column A that have the same value on column B 当 B 列中存在两个值且 A 列中存在相同值时进行计数 - Count when two values exist in column B with the same value in column A Excel:为A列的相同值计算B列的平均值,其中仅存在B列的所有值对应于相同的A值 - Excel: calculate average of column B for same values of column A where only all values of column B to corresponding same A value is present VBA-如果A列中的名称相同,则B列中的总和值 - VBA- Sum values in Column B if name in column A is the same 如果 A 列等于某个值,则在 B 列中生成一个值。检查 A 列中的每个单元格 --> 在 B 列中的每个单元格中生成一个值 - If column A equals a certain value, generate a value in column B. Check each cell in column A --> Generate a value in each cell in column B 当第A列(帐户代码)相同时,第B列的总和(借方)[Excel] - SUM COLUMN B (Debit) when COLUMN A (Account Code) is the Same [Excel] 复制行col。 A到Col。 B,但保留B列中的当前单元格值。(Excel) - Copy rows col. A to col. B, butkeep current cell value in Column B. (Excel) 当B列中出现相同的对应值时,计算A列中的值的时间差 - Calculating time differences in values from column A when the same corresponding value appears in column B excel-如果a列的值相同,如何用值填充b列 - excel - how to fill column b with values if value of column a is the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM