Google Groups Home
Help | Sign in
Message from discussion Find and delete duplicate rows macro req please.
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Jerry Hunt  
View profile
 More options Nov 19 1997, 3:00 am
Newsgroups: microsoft.public.excel.programming
From: jh...@Hellobots.geocities.com (Jerry Hunt)
Date: 1997/11/19
Subject: Re: Find and delete duplicate rows macro req please.

Below is a sub that will test for and delete duplicates. This process
is normally a bit easier to do if the data is sorted, but since your
example was clearly not sorted data, the following sub is designed to
work with unsorted data. This particular sub (based on your data)
assumes that cells A:D are the relevant cells. What the sub does, is
to concatenate these four cells for testing purposes. This sub assumes
that the range to be evaluated starts in cell A2 - modify accordingly.

Sub DeleteDuplicates()

Dim LastRow As Integer
Dim TestRow As Variant
Dim ReturnCell As Range

'   Go to start of data range, get last row number and set first
        return marker
    ActiveSheet.Range("A2").Select
    LastRow = ActiveCell.End(xlDown).Row
    Set ReturnCell = ActiveCell

'   Begin overall loop
    For Row = 2 To LastRow

'   Exit loop if next row is blank
    If ActiveCell.Offset(1, 0) = " " Then Exit Sub

'   Concatenate current row and capture address of ActiveCell
    TestRow = ActiveCell.Text & ActiveCell.Offset(0, 1).Text & _
        ActiveCell.Offset(0, 2).Text & ActiveCell.Offset(0, 3).Text

'   Move to next row to begin testing for duplicates
    ActiveCell.Offset(1, 0).Select

'   Loop through remaining rows and delete duplicates of current row
    For testrows = ActiveCell.Row To LastRow

'   Concatenate current row
    ThisRow = ActiveCell.Text & ActiveCell.Offset(0, 1).Text _
        & ActiveCell.Offset(0, 2).Text & ActiveCell.Offset(0, 3).Text

'   Compare value of current row against TestRow
'       and delete row if same
    If ThisRow = TestRow Then
        Selection.EntireRow.Delete Shift:=xlUp
        LastRow = LastRow - 1

'   If not equal, move to next row
        Else ActiveCell.Offset(1, 0).Select
    End If

    Next

'   Go back to ReturnCell, advance to next row, reset ReturnCell
    ReturnCell.Select
    ActiveCell.Offset(1, 0).Select
    Set ReturnCell = ActiveCell

    Next

End Sub

=========================================================================== ======1

On Wed, 19 Nov 1997 07:55:06 GMT, higr...@xtra.co.co.nz wrote:
>How can I create a macro that will find and delete rows that contain the exact same data as another
>row.

>eg:
>          A               B             C            D
>3      this            row           is           fine
>"
>"
>"
>27   this             row           is           fine            

>I need to delete row 27 as it duplicates row 3, if all entries (ie: A3:D3 = A27:D27) then delete one
>of the rows.

>thanks if you can help me

>Hig  

Spam buster:
To send me e-mail, remove the "hello..." from my e-mail address (I
hate spambots).

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google