Removing Duplicate Rows

Written by Allen Wyatt (last updated September 15, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003


5

When you are working with a large data table, it is not uncommon for the table to contain what is essentially duplicate information. To process the information in the table, you may want to remove any of the rows you consider duplicate, thereby paring down the amount of information you need to process.

For instance, let's say that the first cell of each row contains a part number. What if you want to delete any rows that have duplicate part numbers in the first cell? If you need this solution, the following macro is for you:

Sub DelDupRows()
    Dim rngSrc As Range
    Dim NumRows As Integer
    Dim ThisRow As Integer
    Dim ThatRow As Integer
    Dim ThisCol As Integer
    Dim RightCol As Integer
    Dim J As Integer, K As Integer

    Application.ScreenUpdating = False
    Set rngSrc = ActiveSheet.Range(ActiveWindow.Selection.Address)

    NumRows = rngSrc.Rows.Count
    ThisRow = rngSrc.Row
    ThatRow = ThisRow + NumRows - 1
    ThisCol = rngSrc.Column
    RightCol = ThisCol + rngSrc.Columns.Count - 1

    'Start wiping out duplicates
    For J = ThisRow To (ThatRow - 1)
        If Cells(J, ThisCol) > "" Then
            For K = (J + 1) To ThatRow
                If Cells(J, ThisCol) = Cells(K, ThisCol) Then
                    Cells(K, ThisCol) = ""
                End If
            Next K
        End If
    Next J

    'Remove rows with empty key cells
    For J = ThatRow To ThisRow Step -1
        If Cells(J, ThisCol) = "" Then
            Range(Cells(J, ThisCol), _
              Cells(J, RightCol)).Delete xlShiftUp
        End If
    Next J
    Application.ScreenUpdating = True
End Sub

The macro works on a selection you make before calling it. Thus, if you need to remove duplicate rows from the range D7:G85, simply select that range and then run the macro. It removes the duplicates from the range D7:D85, and then removes all rows in D7:G85 (four columns per row) for which the cell in column D is blank.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2108) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Basing Headers and Footers on the Previous Section

Word treats the headers and footers in a document independently, based on the section in which they appear. This means ...

Discover More

Letters Turn Into Squares

Imagine that you are typing away, and all of a sudden your beautiful prose turns into a series of small rectangles that ...

Discover More

Calculating a Future Date

Need to figure out a date a certain number of days, weeks, months, or years in the future? It's easy to do using the ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Deleting Blank Columns

Import data from another program, and you could end up with a lot of blank columns in your data. Here's the quickest way ...

Discover More

Quickly Deleting Cells

If you need to delete a cell, the Delete key won't do it. (That only clears the contents of a cell; it doesn't delete the ...

Discover More

Stopping the Deletion of Cells

You can delete cells from a worksheet, and Excel will move the remaining cells either to the left or upwards. Deletions, ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

2018-10-03 12:22:47

Willy Vanhaelen

@Michael
Whether you do the Y loop top to borttom or reverse doesn't matter.

@Barry
You probably didn't test the macro because then you would have found it working quite well. In the figure herafter I explain how it works. There is even a possibility to increase the speed: simply insert "Exit For" just before the "End If" line. When the Y loop deletes a duplicate the row bolow takes its place but that has already been tested so it has no sence to continue the loop.
(see Figure 1 below)

Figure 1. 


2018-09-24 03:46:34

Michael (Micky) Avidan

@Barry,
Try:
-----------------------------
For Y = X-1 To 1 Strp -1
-----------------------------
Micky


2018-09-23 07:17:56

Barry

@Willy

I think this has a bug in it. When you delete the row everything below moves up 1 row but Y doesn't adjust also so as it stands the new row Y will not be tested as a possible duplicate. It gets interesting if X is greater than Y when a row is deleted.

The code also deems a duplicate if the first column is the same even if other columns i the row are different. This may be acceptable in some circumstances but is depends on the applications definition of what is a duplicate.


2018-09-22 14:06:54

Willy Vanhaelen

You don't need three loops. In the second loop you can delete the duplicate row at once.

Here is a drastically simplified macro that does the job as well:

Sub DelDupRows()
Dim X As Long, Y As Long
Application.ScreenUpdating = False
With Selection
For X = .Rows.Count To 1 Step -1
For Y = 1 To X - 1
If .Cells(X, 1) = .Cells(Y, 1) Then
.Range(Cells(X, 1), Cells(X, .Columns.Count)).Delete xlShiftUp
End If
Next Y
Next X
End With
Application.ScreenUpdating = True
End Sub


2018-09-21 06:12:50

Thomas Papavasiliou

Thanks for your tips. An option for keeping the first or last matching data may be quite useful


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.