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

Special Characters in Pattern Matching

The most powerful search engine in Word use pattern matching, but the way you specify special characters in a ...

Discover More

Compound Page Numbering

Simple page numbering is easy to add to your documents. More complex numbering (such as two numbering schemes in the same ...

Discover More

Keeping Word Open after Closing Documents

Usually when you are done working on a document, you want to close Word completely and move on to something else. There may ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

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

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

Removing Duplicate Cells

If you need to often delete duplicate items from a list, then you'll love the macro presented in this tip. It makes quick ...

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}] 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 4 + 1?

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


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.