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

Easily Entering Dispersed Data

Need to enter information into a bunch of cells that aren't anywhere near each other in the worksheet? Here's a handy way to ...

Discover More

Stopping Excel from Converting UNC Paths to Mapped Drives

Did you know that if you create a link that uses a UNC path, Excel could rewrite that path to something entirely different? ...

Discover More

Deleting a Hyperlink

Hyperlinks can be helpful in some worksheets but bothersome in others. Here's how to get rid of any hyperlinks you don't ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 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 to ...

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

Clearing and Deleting Cells

When you want to remove information from a worksheet, you can either clear cells or delete cells. This tip examines the ...

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 nine more than 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.

Links and Sharing