Consulting

Results 1 to 8 of 8

Thread: Delete Duplicate Row

  1. #1
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location

    Delete Duplicate Row

    Hi

    does anyone have a macro that will check all rows in column D and delete any duplicates?
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  2. #2
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Hoopsah,

    Have you looked in the Knowledge Base (KBase in toolbar, above) and/or search the threads, here?

    Cheers!
    Ron
    Windermere, FL

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Does delete trhe cell mean blank out the contents, or shunt the rets of the row one cell left?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Or, do you want to delete the entire row whenever cell D[this row] equals (is identical to the value of cell D[any preceding row]?

    Cheers,
    Ron
    Windermere, FL

  5. #5
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Hi

    I was hoping to delete the row below completely.

    I have now completed the task by putting a formula in column E simply doing a =IF(D2=D1,"DUP",0)

    Then simply deleted anything that had DUP there and so on.

    But going forward it would be nice to know if there was a macro that could do this

    Cheers
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sub Deletedups()
    Dim LastRow As Long
    Dim rng As Range
    With ActiveSheet
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
            .Columns("E").Insert
            .Range("E1").Value = "Flag"
            .Range("E2").Resize(LastRow - 1).Formula = "=IF(D2=D1,""DUP"","""")"
            Set rng = .Columns("E")
            rng.AutoFilter Field:=1, Criteria1:="DUP"
    On Error Resume Next
            Set rng = .Range("E1").Resize(LastRow - 1).SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not rng Is Nothing Then rng.EntireRow.Delete
            .Columns("E").Delete
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,207
    Location
    Heres one that would just delete the rows based on the duplicates found in column "D"

    Sub DelDups()
    Dim Col As Integer
    Dim r As Long
    Dim C As Range
    Dim V As Variant
    Dim Rng As Range
    On Error GoTo EndSub
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Set Rng = Range("D1:D" & Range("D65536").End(xlUp).Row)
    For r = Rng.Rows.Count To 1 Step -1
        V = Rng.Cells(r, 1).Value
        If V = "" Then Rng.Rows(r).EntireRow.Delete
        If Application.WorksheetFunction.CountIf(Rng.Columns(1), V) > 1 Then
            Rng.Rows(r).EntireRow.Delete
        End If
    Next r
    EndSub:
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2404, Build 17531.20128

  8. #8
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Works Perfectly!

    Thanks guys I have now saved this macro to my own library - one of these days someone will ask me to do this chore again - lol

    Cheers

    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

Posting Permissions

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