Remove duplicate rows Excel VBA

2019-09-25 08:59发布

I am writing a script in VBA that would remove duplicate rows in an Excel spreadsheet. However, I want it to delete duplicate rows considering only information in two columns.

In other words, I have a table with the range B:F. I want the script to remove duplicate rows considering, for each row, only the values on columns D and E. In the end, only rows which simultaneously have the exact same values on columns D and E - regardless of other columns - will be removed. How could I go about doing this? Thank you

1条回答
贪生不怕死
2楼-- · 2019-09-25 09:22

Here is an example that does this.

Make sure you run it with the sheet you want to use up:

Sub DeleteDupes()
Dim x
For x = Cells(Rows.CountLarge, "D").End(xlUp).Row To 1 Step -1
    If Cells(x, "D") = Cells(x, "E") Then
        'This line deletes the row:
        Cells(x, "D").EntireRow.Delete xlShiftUp
        'This line highlights the row to show what would be deleted;
        'Cells(x, "D").EntireRow.Interior.Color = RGB(230, 180, 180)
    End If
Next x
End Sub

Results of highlighting:

Results

Results of Delete:

Results2

查看更多
登录 后发表回答