excel vba,如果A列相同,则对应b列数据求和。

excel vba,如果A列相同,则对应b列数据求和。A 1
A 2
A 3
B 2
B 3
B 4
C 3
C 4
最后输出:
A 6
B 9
C 7
这样

参考:
Sub SumCalculation()
Dim FirstRow As Long, LastRow As Long
Dim MyCpt As Long
Dim F As Range
Dim I As Long
Dim MyFormula As String
Application.ScreenUpdating = False
FirstRow = 2
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range(Cells(FirstRow, "B"), Cells(LastRow, "B")).ClearContents

For I = FirstRow To LastRow
If (Cells(I, "A") <> "") Then
MyFormula = "COUNTIF(A" & FirstRow & ":A" & I & ",A" & I & ")"
MyCpt = Evaluate(MyFormula)
If (MyCpt = 1) Then
MyFormula = "SUMPRODUCT((A" & FirstRow & ":A" & LastRow & "=A" & I & ")*(C" & FirstRow & ":C" & LastRow & "))"
Cells(I, "B") = Evaluate(MyFormula)
End If
End If
Next I
Application.ScreenUpdating = True
End Sub
温馨提示:答案为网友推荐,仅供参考
相似回答