+ Reply to Thread
Results 1 to 8 of 8

Remove duplicate rows

Hybrid View

  1. #1
    Registered User
    Join Date
    11-30-2006
    Posts
    67

    Remove duplicate rows

    Hi.

    I have found this at:

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


    Public Sub DeleteDuplicateRows()
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' DeleteDuplicateRows
    ' This will delete duplicate records, based on the Active Column. That is,
    ' if the same value is found more than once in the Active Column, all but
    ' the first (lowest row number) will be deleted.
    '
    ' To run the macro, select the entire column you wish to scan for
    ' duplicates, and run this procedure.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Dim R As Long
    Dim N As Long
    Dim V As Variant
    Dim Rng As Range
    
    On Error GoTo EndMacro
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    
    Set Rng = Application.Intersect(ActiveSheet.UsedRange, _
                        ActiveSheet.Columns(ActiveCell.Column))
    
    Application.StatusBar = "Processing Row: " & Format(Rng.Row, "#,##0")
    
    N = 0
    For R = Rng.Rows.Count To 2 Step -1
    If R Mod 500 = 0 Then
        Application.StatusBar = "Processing Row: " & Format(R, "#,##0")
    End If
    
    V = Rng.Cells(R, 1).Value
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Note that COUNTIF works oddly with a Variant that is equal to vbNullString.
    ' Rather than pass in the variant, you need to pass in vbNullString explicitly.
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    If V = vbNullString Then
        If Application.WorksheetFunction.CountIf(Rng.Columns(1), vbNullString) > 1 Then
            Rng.Rows(R).EntireRow.Delete
            N = N + 1
        End If
    Else
        If Application.WorksheetFunction.CountIf(Rng.Columns(1), V) > 1 Then
            Rng.Rows(R).EntireRow.Delete
            N = N + 1
        End If
    End If
    Next R
    
    EndMacro:
    
    Application.StatusBar = False
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    MsgBox "Duplicate Rows Deleted: " & CStr(N)
    
    End Sub


    But what i want it to do is to look through two columns like these:

    Cat Number

    Aa 0001
    Aa 0001
    Aa 0001
    Aa 0001
    Aa 0001
    Aa 0002
    Aa 0002
    Aa 0002
    Aa 0002
    Aa 0003
    Aa 0004
    Aa 0005
    Aa 0005
    Aa 0005
    Aa 0005
    Aa 0005
    Aa 0006
    Ab 0001
    Ab 0001
    Ab 0001
    Ab 0001
    Ab 0001
    Ab 0001
    Ab 0001
    Ab 0001
    Ab 0002
    Ab 0002
    Ab 0002
    Ab 0002
    Ab 0002
    Ab 0003
    Ab 0003
    Ab 0003
    Ab 0003
    Ab 0003
    Ab 0004
    Ab 0004
    Ab 0004
    Ab 0004
    Ab 0005
    Ab 0005
    Ab 0005
    Ab 0005
    Ab 0005
    Ab 0005
    Ab 0005
    Ac 0001
    Ac 0001
    Ac 0001
    Ac 0001
    Ac 0001
    Ac 0001
    Ac 0001
    Ac 0002
    Ac 0002
    Ac 0002
    Ac 0002
    Ac 0002
    Ac 0002
    Ac 0003
    Ac 0003
    Ac 0003
    Ac 0003
    Ac 0003
    Ac 0003
    Ac 0004
    Ac 0004
    Ac 0004
    Ac 0004
    Ac 0004
    Ac 0004
    Ac 0004


    and have a result like this:

    Cat Number

    Aa 0001
    Aa 0002
    Aa 0003
    Aa 0004
    Aa 0005
    Ab 0001
    Ab 0002
    Ab 0003
    Ab 0004
    Ab 0005
    Ac 0001
    Ac 0002
    Ac 0003
    Ac 0004

    Is It possible to edit the macro mentioned or is there a complete (better?) way?

    Hope you can help me out on this on...

    Nicolai
    Last edited by Vestlink; 03-01-2010 at 06:01 AM.

  2. #2
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Remove duplicate rows

    Supposing your data is in one colmn, select it, go to Data Filter Advanced filter and follow the wizard - Check " unique records only"

  3. #3
    Registered User
    Join Date
    11-30-2006
    Posts
    67

    Re: Remove duplicate rows

    Quote Originally Posted by arthurbr View Post
    Supposing your data is in one colmn, select it, go to Data Filter Advanced filter and follow the wizard - Check " unique records only"
    Actually the data are in two cloumns next to each other.

    Nicolai

  4. #4
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Remove duplicate rows

    Quote Originally Posted by Vestlink View Post
    Actually the data are in two cloumns next to each other.

    Nicolai

    Works if data is in two or more columns also

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Remove duplicate rows

    maybe..

    Sub ptest()
        Dim pcount, i!, klist(), elist, test$
        With Range("a1", Range("a" & Rows.Count).End(xlUp)).Resize(, 2)
            elist = .Value
        ReDim klist(UBound(elist, 1), 1 To 2)
        With CreateObject("Scripting.Dictionary")
            .CompareMode = vbTextCompare
            For i = 1 To UBound(elist, 1)
                test = elist(i, 1) & elist(i, 2)
                If Not IsEmpty(test) Then
                    If Not .exists(test) Then
                     .Add test, pcount
                      klist(pcount, 1) = elist(i, 1)
                      klist(pcount, 2) = elist(i, 2)
        pcount = pcount + 1
                      End If
             End If
            Next
           End With
     .ClearContents
     End With
    Range("A1").Resize(pcount, 2).Value = klist
     End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  6. #6
    Registered User
    Join Date
    11-30-2006
    Posts
    67

    Re: Remove duplicate rows

    Thanx all.

    It did the trixx.

    Nicolai

  7. #7
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    Re: Remove duplicate rows

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Remove duplicate rows

    if you have 2007xl you could use
    .Range("A1:A10").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes

+ 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