Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

Remove duplicate rows Excel VBA

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...