根据另一列删除重复项,然后添加删除的重复项vba的相应值

时间:2018-01-16 16:37:04

标签: excel-vba vba excel

我需要帮助删除重复项并添加相应的值 - 这取决于它们是否与其他列重复。在这种情况下,如果部件号在同一工单下重复,则删除重复 - 但是,添加所有值。我将需要避免从另一个工单号中删除重复项。

enter image description here

1 个答案:

答案 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