Removing Duplicate Rows

by Allen Wyatt
(last updated September 23, 2016)

3

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.

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

Displaying Excel's Developer Tab

The Developer tab of the ribbon is the gateway to many advanced features in Excel, including those features related to ...

Discover More

Changing an Invalid Autosave Folder

Excel allows you to specify where it stores various files used by the program. One location you can specify is where Autosave ...

Discover More

Getting Rid of 8-Bit ASCII Characters

When working with data created outside of Excel, you may need to check that data to make sure it contains no unwanted ...

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)

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

Deleting Everything Except Formulas

Need to get rid of everything in a worksheet except the formulas? It's easier to make this huge change than you think it is.

Discover More

Quickly Deleting Rows and Columns

Deleting rows or columns is easy when you use the shortcut described in this tip. Just select the rows or columns and then ...

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 for this tip:

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 seven minus 6?

2013-01-27 03:09:37

Soroh

Perfect Michael, this will save me lots of time... and is short.


2013-01-26 05:53:33

Michael Avidan - MVP

The previous suggested code still "moves-up" the remaining cells.

If one wants to delete the ENTIRE Row - one command - within the IF statement - should be replaced with the following:

Cells(R, FC).EntireRow.Delete

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


2013-01-26 05:48:21

Michael Avidan - MVP

As far as I understand the sentence: "if you want to delete any rows that have duplicate part numbers in the first cell" - it means to delete the ENTIRE ROW, in the worksheet, and not moving up the remaining data – therefor, to my opinion, the following piece of Code is pretty much shorter and easier to understand:

Sub DeleteDuplicates()
Application.ScreenUpdating = False
FC = Selection.Column
CC = Selection.Columns.Count
FR = Selection.Row
LR = Cells(Rows.Count, Selection.Column).End(xlUp).Row
For R = LR To FR Step -1
If Application.CountIf(Range(Cells(FR, FC), Cells(LR, FC)), Cells(R, FC).Text) > 1 Then
Cells(R, FC).Resize(1, CC).Delete xlShiftUp
End If
Next
Application.ScreenUpdating = True
End Sub
---------------

Michael Avidan
“Microsoft®” MVP – Excel
ISRAEL


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.

Links and Sharing
Share