Results 1 to 8 of 8

Remove duplicate rows

Threaded 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.

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