答案 0 :(得分:0)
尝试以下操作,它会在右侧添加一列,使用公式到SumIF,然后将结果粘贴为值并删除列C以将数据带到正确的列,然后删除重复项以获得预期的摘要:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data from Column A
Range("D2").FormulaR1C1 = "=SUMIF(C[-2],RC[-2],C[-1])"
'enter a sumif formula to add all with same item number
Range("D2:D" & LastRow).FillDown 'fill the formula to the last row
Range("D2:D" & LastRow).Copy 'copy
Range("D2:D" & LastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'paste as values
Range("C2:C" & LastRow).Delete Shift:=xlToLeft
'delete column C and bring to the left the values calculated by formula
Range("$A$1:$D$" & LastRow).RemoveDuplicates Columns:=Array(1, 2, 3, 4), Header:=xlYes
'remove duplicates
End Sub