Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Removing Duplicate Rows

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.

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Soroh    27 Jan 2013, 03:09
Perfect Michael, this will save me lots of time... and is short.
Michael Avidan - MVP    26 Jan 2013, 05:53
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
Michael Avidan - MVP    26 Jan 2013, 05:48
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
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.