+ Reply to Thread
Results 1 to 7 of 7

Finding Duplicates & Deleting the ROw

Hybrid View

samtwilliams Finding Duplicates & Deleting... 09-19-2007, 02:52 PM
samtwilliams any ideas? 09-19-2007, 03:22 PM
VBA Noob Maybe these links will help ... 09-19-2007, 03:26 PM
jasoncw You could try a VBA loop: ... 09-19-2007, 03:33 PM
avveerkar Jason, Using countif to... 09-19-2007, 03:59 PM
jasoncw Hi, A V. Thanks for looking... 09-19-2007, 05:07 PM
royUK Select the range to check... 09-19-2007, 03:26 PM
  1. #1
    Registered User
    Join Date
    08-18-2007
    Posts
    54

    Finding Duplicates & Deleting the ROw

    I have a list of 6000 items that have data in multiple columns.

    I want to be able to find duplicates in column A and delete the Row

    I have tried everything from Advanced FIlters to Macros and both don't seem to work.

    Anyone have a solution?

  2. #2
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    any ideas?

  3. #3
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe these links will help

    http://www.ozgrid.com/VBA/RemoveDuplicates.htm

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=135

    http://www.cpearson.com/excel/deleting.htm

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  4. #4
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    You could try a VBA loop:
    Dim i As Long, lastRow As Long
    
    lastRow = Range("A" & Cells.Rows.Count).End(xlUp).Row
    
    For i = lastRow To 1 Step -1
        If Application.Countif(Range("A1:A" & lastRow), Range("A" & i)) > 1 Then _
            Rows(i).EntireRow.Delete
    Next i
    Jason

  5. #5
    Forum Contributor
    Join Date
    01-10-2006
    Location
    Ahmedabad, India
    MS-Off Ver
    Office 2000
    Posts
    346
    Quote Originally Posted by jasoncw
    You could try a VBA loop:
    Dim i As Long, lastRow As Long
    
    lastRow = Range("A" & Cells.Rows.Count).End(xlUp).Row
    
    For i = lastRow To 1 Step -1
        If Application.Countif(Range("A1:A" & lastRow), Range("A" & i)) > 1 Then _
            Rows(i).EntireRow.Delete
    Next i
    Jason
    Jason,

    Using countif to detect duplicate is very good idea and very efficient. However there are two issues with this,
    As the loop progresses and starts deleting the desired rows, the source range should progressively shorten but since lastRow value is fixed at the begining of the loop, range extends to the lower rows which were outside the range initially. Not desirable. Of course in this case it does not matter because we are assuming that all the bottom rows outside the range are blank.
    2. This macro will not delete both the rows which have same value. It will delete one and retain the other. To explain let us say that row 10 and 6 have duplicate values. The loop first deletes row 10 but when it reaches row 6 it finds no duplicate because row 10 was already deleted.
    To avoid these problems it might be a good idea to use the loop to first flag the rows ( eg color the duplicate cells ) and then run a loop to delete the flagged rows.

    A V Veerkar

  6. #6
    Forum Expert
    Join Date
    12-29-2004
    Location
    Michigan, USA
    MS-Off Ver
    2013
    Posts
    2,208
    Quote Originally Posted by avveerkar
    Jason,

    Using countif to detect duplicate is very good idea and very efficient. However there are two issues with this,
    As the loop progresses and starts deleting the desired rows, the source range should progressively shorten but since lastRow value is fixed at the begining of the loop, range extends to the lower rows which were outside the range initially. Not desirable. Of course in this case it does not matter because we are assuming that all the bottom rows outside the range are blank.
    2. This macro will not delete both the rows which have same value. It will delete one and retain the other. To explain let us say that row 10 and 6 have duplicate values. The loop first deletes row 10 but when it reaches row 6 it finds no duplicate because row 10 was already deleted.
    To avoid these problems it might be a good idea to use the loop to first flag the rows ( eg color the duplicate cells ) and then run a loop to delete the flagged rows.

    A V Veerkar
    Hi, A V. Thanks for looking at my code and critiquing it.

    1. Yes, I see what you mean. I should have taken the shrinking range into account.

    2. I was under the impression that he did want to leave one of the duplicates in place. Maybe not, though. Leaving one of the duplicates was intentional.

    Thanks.

    Jason

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Select the range to check then run this macro

    Option Explicit
    
    Sub DeleteDuplicateEntries()
       Dim rCl As Range
       Dim rClDupe As Range
       Dim rRng As Range
       Dim N As Long
       With ActiveSheet
       Application.ScreenUpdating = False
       N = 0
       For Each rCl In Selection
            '1st loop - (to speed things up ignore any empty rCls)
           If rCl <> Empty Then
               For Each rClDupe In Selection
                    '2nd loop - compare non-empty rClDupe values
                    'and clear contents if it's a duplicated value
                   If rClDupe <> Empty And _
                   rClDupe.Value = rCl.Value And _
                   rClDupe.Address <> rCl.Address Then
                       rClDupe.ClearContents
                       'delete the row
    '                   rClDupe.EntireRow.Delete
                       N = N + 1
                   End If
               Next rClDupe
           End If
       Next
       End With
       Application.ScreenUpdating = True
       MsgBox "There were " & N & " duplicated entries deleted"
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1