[英]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 : 变成这个:
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.