+ Reply to Thread
Results 1 to 5 of 5

Thread: Remove unique rows and keep duplicates

  1. #1
    Registered User
    Join Date
    10-26-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Remove unique rows and keep duplicates

    Hello,

    Currently, my spreadsheet is advanced filtered to remove duplicates considering both columns. My goal is to have a list of Names (Column B) that have a duplicate in Column B, but a different ID in Column A. All of the names in Column B that do no have a duplicate in Column B, need to be removed. I tried running a macro from this forum, but received an error message.

    Attached is a sample of current and goal data (remove non-duplicated names in red font):

    Current
    ID Name
    26594 17-amino acid co
    39757 17-amino acid co
    20084 18-amino acid
    311 2-propanol

    3725 2-pen
    8300 2-pen
    23380 20/20 artficl tear
    2005 222
    2236 222

    Goal
    ID Name
    26594 17-amino acid co
    39757 17-amino acid co
    3725 2-pen
    8300 2-pen
    2005 222
    2236 222

    Any help is appreciated.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Lake County, Illinois
    MS-Off Ver
    MS Office 2010, 2007 and 2002
    Posts
    1,165

    Re: Remove unique rows and keep duplicates

    This solution is for Excel versions earlier than 2007.
    First, I would do a conditional format on all duplicates. Use the formula found in this link on Column B.

    http://office.microsoft.com/en-au/ex...001136616.aspx

    then create a UDF as shown here

    http://www.datapigtechnologies.com/f...rtonColor.html

    Once you have sorted the file by color, you can then delete the appropriate rows very quickly.

    For 2007 users, use conditional format wizard, sort and delete.

    Alan

  3. #3
    Registered User
    Join Date
    10-26-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Remove unique rows and keep duplicates

    Thank you Alan,

    I will give it a try.

    Terry

  4. #4
    Registered User
    Join Date
    08-01-2010
    Location
    American in Rovereto, Italy
    MS-Off Ver
    Excel 2010 home & Excel 2007 work
    Posts
    40

    Re: Remove unique rows and keep duplicates

    Hello Terry,
    I wrote a function to move duplicates to another sheet.
    Take a look, maybe it will suit your needs.

    Gordon in Rovereto Italy

    Option Explicit
    
    Function MoveALL_Dupes(ByVal whatColumn As Long) As Boolean
    ' Uses a counting formula together with the autofilter
    ' to find and Move ALL duplicates from the Activesheet
    ' to new worksheet.
    '
    ' This is not the opposite of unique
    '
    ' In some cases it's desirable to remove ALL duplicates
    ' not just the 2nd occurence.
    ' because human intervention Is required to fix the problem.
    ' How can a computer know if the 1st or 2nd is the correct one.
    ' it's a 50/50 random chance that the 1st occurence is correct
    '
    
    Dim wksTemp As Worksheet
    Dim wkshtDupes As Worksheet
    Dim rng2Check As Range
    Dim rngTop As Range
    Dim rngBottom As Range
    Dim rngCountFormula As Range
    Dim iLastRow As Long
    Dim shtNbr As Long
    
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        ActiveSheet.Select
        Set wksTemp = ActiveSheet
        
        iLastRow = wksTemp.Cells(65536, whatColumn).End(xlUp).Row
        
        Columns(whatColumn + 1).Insert Shift:=xlToRight
        Cells(1, whatColumn + 1).Value = "Duplicate List"
        
        'Set the range to separate duplicates from unique
        Set rngTop = Cells(2, whatColumn)
        Set rngBottom = Cells(iLastRow, whatColumn)
        Set rng2Check = wksTemp.Range(rngTop, rngBottom)
        Set rngCountFormula = wksTemp.Range(rngTop.Offset(0, 1), rngBottom.Offset(0, 1))
        
        'Add a workbook name for it so we can use it in the formula
        ActiveWorkbook.Names.Add Name:="rngCheck", _
            RefersTo:=rng2Check, Visible:=True
            
        'Insert formula to count duplicate account numbers
        rngCountFormula.FormulaR1C1 = "=COUNTIF(rngCheck,""=""&RC[-1])"
        rngCountFormula.Value = rngCountFormula.Value
        
        Cells(1, whatColumn).CurrentRegion.Columns.AutoFit
        Cells(1, whatColumn).CurrentRegion.AutoFilter
        
        'Make a copy of the data to a new workbook
        shtNbr = wksTemp.Index
        Sheets(shtNbr).Select
        Sheets(shtNbr).Copy After:=Worksheets(Worksheets.Count)
        Set wkshtDupes = ActiveSheet
        wkshtDupes.Name = "Duplicates_" & Worksheets.Count
        
        'Set autofilter to non duplicates then delete them.
        Cells(1, whatColumn).CurrentRegion.AutoFilter Field:=whatColumn + 1, Criteria1:="=1", Operator:=xlAnd
        Range(rngTop.Address, rngTop.End(xlDown).Address).EntireRow.Delete
        
        'Switch off the autofilter and remove the duplicate column
        Selection.AutoFilter
        Columns(whatColumn + 1).Delete Shift:=xlToLeft
        
        'Go Back to the orginal sheet to delete the duplicates
        wksTemp.Select
        
        'Set autofilter to duplicates then delete them.
        Cells(1, whatColumn).CurrentRegion.AutoFilter Field:=whatColumn + 1, Criteria1:="<>1", Operator:=xlAnd
        Range(rngTop.Address, rngTop.End(xlDown).Address).EntireRow.Delete
        
        'Switch off the autofilter and remove the duplicate column
        Selection.AutoFilter
        Columns(whatColumn + 1).Delete Shift:=xlToLeft
        
        'Report back a success
        MoveALL_Dupes = True
    End Function
    Attached Files Attached Files
    Gordon in Rovereto, Italy

  5. #5
    Forum Guru martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    10,798

    Re: Remove unique rows and keep duplicates

    simplest is to count them .
    in a spare column on the same row assuming data starts in b2
    =countif(B:B,B2)>1
    auto fill down to end of data in col b
    now filter that column by false
    you can now delete those rows in on go
    or else filter on true and copy visible rows to somewhere else to work on

    on re -reading this i see you are already using advanced filter since you know how to do it why not reverse the criteria so as to filter on those you dont want, then simply delete the entire lot of visible rows
    Last edited by martindwilson; 11-02-2010 at 09:38 AM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and a dabbler in Cisco
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

+ 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.2.0