【发布时间】:2019-05-10 17:55:32
【问题描述】:
我的数据如下: 样本数据:
A B C Result:
DG M 1 DG M 5
DG M 2 KH M 9
DG M 2 SG C 7
KH M 4 KH M 5
KH M 5 DG M 5
SG C 6
SG C 1
KH M 3
KH M 2
DG M 5
我这里有 3 列,如果 A 列和 B 列中的行与前一行相同,我希望总结值。
以下是我从其他人那里引用的代码。但是代码似乎只有一个条件,我想寻求一种方法来添加另一个条件。谢谢。
Sub MG()
Dim Rng As Range, Dn As Range, n As Double, nRng As Range
Set Rng = Worksheets("sheet1").Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
.Add Dn.Value, Dn
Else
If nRng Is Nothing Then Set nRng = Dn Else Set nRng = Union(nRng, Dn)
.Item(Dn.Value).Offset(, 3) = .Item(Dn.Value).Offset(, 3) + Dn.Offset(, 3)
End If
Next
If Not nRng Is Nothing Then nRng.EntireRow.Delete
End With
End Sub
【问题讨论】: