Excel VBA。总和重复

时间:2015-06-04 08:34:55

标签: excel vba

我有这张桌子:

Column1 | Column2 
-----------------
Name1   |  3,0 
Name1   |  2,0 
Name2   |  5,0 
Name2   |  6,0 
etc...

我需要VBA宏,这将导致:

Column1 | Column2 
-----------------
Name1   |  5,0 
Name2   |  11,0 

步骤:
1.总结第2列 2.删除重复项

1 个答案:

答案 0 :(得分:0)

尝试以下

将新列表放在D和E中,假设列表在当前工作表和A:B

Sub te()

Range("D1:" & Range("E1").End(xlDown).Address).ClearContents
Range("A1:" & Range("A1").End(xlDown).Address).Copy Destination:=Range("D1")
Range("D1:" & Range("D1").End(xlDown).Address).RemoveDuplicates Columns:=1, Header:=xlNo

For Each cell In Range("D1:" & Range("D1").End(xlDown).Address)
    temp = 0
    For Each c In Range("A1:" & Range("A1").End(xlDown).Address)
        If cell.Value = c.Value Then
            temp = temp + c.Offset(0, 1).Value
        End If
    Next

    For Each c In Range("D1:" & Range("D1").End(xlDown).Address)
        If cell.Value = c.Value Then
             c.Offset(0, 1).Value = temp
        End If
    Next
Next

End Sub