根据两列的公共值删除两列分组列的三列

时间:2015-03-11 23:33:06

标签: excel excel-vba vba

我在一个工作表中有两组三个分组列。这延伸了30000行。示例数据如下:

我想要一个宏像这样的代码: 代码:

when (old serial)=(new serial) then delete (new code) AND (new serial) AND(new amount)

old code    old serial  old amount  new code    new serial  new amount
  1           11             100       2            12         2000
  2           12             200       1            11         1000
  3           13             300       2            200        2000
  4           14             400       4            400        4000

算法后的表格如下:

old code    old serial  old amount  new code    new serial  new amount
1             11             100        2            200      2000
2             12             200        4            400      4000
3             13             300            
4             14             400        

1 个答案:

答案 0 :(得分:0)

假设您的数据看起来像这样且“新串行”列中的所有条目都是唯一的,这就是解决方案:

enter image description here

Sub test()
Set ExcelAppl = CreateObject("Excel.Application")
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
Set ws = wb.Worksheets(1)
Set Rng = ws.UsedRange
RowCount = Rng.Rows.Count
For i = 1 To RowCount
OldSerial = Cells(i, 2).Value
'assign the column range of "new serial" that you want to find
With ws.Range("E:E")
Set C = .Find(OldSerial, LookIn:=xlValues)
If Not C Is Nothing Then
rowNumber = C.Row
colNumber = C.Column
'delete new serial
Cells(rowNumber, colNumber).Value = ""
'delete new code
Cells(rowNumber, colNumber - 1).Value = ""
'delete new amount
Cells(rowNumber, colNumber + 1).Value = ""
End If
End With
Next
Set Rng = Nothing
Set ws = Nothing
Set wb = Nothing

End Sub

输出将是:

enter image description here