Excel / VBA通​​过交叉引用2个不同的工作表删除重复的行,然后删除1行

我有2个单独的工作表,我们称他们为工作表A,工作表B.我有工作表B中的数据也在工作表A中。我想find那些行相同,并从表B中删除它们。

我不能合并这两个工作表,并使用filter,因为我正在做dynamicSQL来查询不同的数据。

每张表都有一个唯一的键列

我对VBAbuild议和Excel公式很好。 只要我不合并床单。

非常感谢你们!

对不起,显然我犯了一个错误。 这里有一个无限循环。 这是本的答案btw。 我刚刚转贴了一个可编辑的版本。

Sub CleanDupes() Dim wsA As Worksheet Dim wsB As Worksheet Dim keyColA As String Dim keyColB As String Dim rngA As Range Dim rngB As Range Dim intRowCounterA As Integer Dim intRowCounterB As Integer keyColA = "A" keyColB = "A" intRowCounterA = 1 intRowCounterB = 1 Set wsA = Worksheets("Sheet2") Set wsB = Worksheets("Sheet1") Do While Not IsEmpty(wsA.Range(keyColA & intRowCounterA).Value) Set rngA = wsA.Range(keyColA & intRowCounterA) intRowCounterB = 1 Do While Not IsEmpty(wsB.Range(keyColB & intRowCounterB).Value) Set rngB = wsB.Range(keyColB & intRowCounterB) If rngA.Value = rngB.Value Then Rows(intRowCounterB).EntireRow.Delete intRowCounterB = intRowCounterB - 1 End If intRowCounterB = intRowCounterB + 1 Loop intRowCounterA = intRowCounterA + 1 Loop End Sub 

 Sub CleanDupes() Dim wsA As Worksheet Dim wsB As Worksheet Dim keyColA As String Dim keyColB As String Dim rngA As Range Dim rngB As Range Dim intRowCounterA As Integer Dim intRowCounterB As Integer Dim strValueA As String keyColA = "A" keyColB = "B" intRowCounterA = 1 intRowCounterB = 1 Set wsA = Worksheets("Sheet A") Set wsB = Worksheets("Sheet B") Do While Not IsEmpty(wsA.Range(keyColA & intRowCounterA).Value) intRowCounterB = 1 Set rngA = wsA.Range(keyColA & intRowCounterA) strValueA = rngA.Value Do While Not IsEmpty(wsB.Range(keyColB & intRowCounterB).Value Set rngB = wsB.Range(keyColB & intRowCounterB) If strValueA = rngB.Value Then 'Code to delete row goes here, but I'm not sure exactly' 'what it is.' wsB.Rows(intRowCounterB).Delete intRowCounterB = intRowCounterB - 1 End If intRowCounterB = intRowCounterB + 1 Loop intRowCounterA = intRowCounterA + 1 Loop End Sub 

这应该让你开始。