正负相抵消(Excel代码集团)
数据源如AB列,达成效果如DE列:
代码如下:
Sub Sample() Dim MyRow As Long, MyTRow As Long, MyBRow As Long Dim MyFind As Range MyBRow = Cells(Rows.Count, 1).End(xlUp).Row Do MyTRow = MyBRow - Application.CountIf(Range("a:a"), Cells(MyBRow, 1)) + 1 For MyRow = MyBRow To MyTRow Step -1 Set MyFind = Cells(MyTRow, 2).Resize(MyBRow - MyTRow + 1, 1).Find(Cells(MyRow, 2) * -1, , , xlWhole) If Not MyFind Is Nothing Then Cells(MyRow, 1).EntireRow.Delete Cells(MyFind.Row, 1).EntireRow.Delete MyBRow = MyTRow + Application.CountIf(Range("a:a"), Cells(MyTRow, 1)) - 1 End If Next MyBRow = MyBRow - Application.CountIf(Range("a:a"), Cells(MyTRow, 1)) Loop Until MyBRow < 3 Set MyFind = Nothing End Sub