+ Reply to Thread
Results 1 to 3 of 3

Delete Duplicate Rows:use VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2009
    Location
    Oxford
    MS-Off Ver
    Excel 2007
    Posts
    86

    Delete Duplicate Rows:use VBA

    I want to use VBA to delete a row if it contains exactly the same data as another. I have been using this:

    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
    
    
    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
    
    
    End Sub

    but it will only delete rows based on data from 1 column. I know this is a simple one - I just can't find the answer. Cheers.

  2. #2
    Registered User
    Join Date
    05-28-2009
    Location
    Oxford
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Delete Duplicate Rows (again)

    Does anyone know? Thanks.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    re: Delete Duplicate Rows:use VBA

    Why not just used AdvancedFilter and tick Unique records only?
    Entia non sunt multiplicanda sine necessitate

+ 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