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

Excel 根据两列的公共值删除两个分组列中的三列,excel,vba,Excel,Vba,我在一个工作表中有两组三个分组列。这条路长达30000行。样本数据如下: 我想要一个宏,就像下面的代码: 代码: 算法后的表格如下: old code old serial old amount new code new serial new amount 1 11 100 2 200 2000 2 12 200 4

我在一个工作表中有两组三个分组列。这条路长达30000行。样本数据如下:

我想要一个宏,就像下面的代码: 代码:

算法后的表格如下:

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        

假设您的数据如下所示,并且“New Serial”列中的所有条目都是唯一的,下面是解决方案:

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

输出将是:


tahnks.raj.以获取您的答案。但此宏在某些情况下无法工作,例如旧序列和新序列不能像我更新的表那样同时出现。您不能开发vb代码来解决此问题吗。!thnx。。。。!这些“旧序列”或“新序列”值是唯一的吗@shahramyes@raj他们是独一无二的。我想删除旧的。您的代码与同一行中的旧序列号和新序列号一样好。但我测试了这一点,例如,当第一行中的旧序列号(11)和第二行中的新序列号(11)无效时。!:)我已经更新了解决方案,现在它将工作,即使你有旧的序列和新的序列在不同的行。但是我对你的要求感到很困惑。你在问题中提到你想删除“新代码”、“新序列号”、“新金额”,但在你之前提到的评论中,你想删除旧的。我的代码应该满足问题的要求。
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