Consulting

Results 1 to 8 of 8

Thread: Solved: Delete duplicated rows

  1. #1
    VBAX Regular
    Joined
    Mar 2007
    Posts
    47
    Location

    Solved: Delete duplicated rows

    Hello!

    I want to check entire sheet and compare every row to find identical rows and then delete one of them. Any advice? Please!

  2. #2
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location

    Delete duplicated rows

    Here is an example that you can try. It is from this forum....although I don't remember who submitted it It is not mine, so I won't take credit it for it. Hopefully you can use/modify to fit your needs.

    [VBA] Sub SortAndMark()

    Dim Rng As Range

    Set Rng = Sheets(1).Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))

    DoSort Rng
    MarkDups Rng


    End Sub[/VBA]

    [VBA] Sub DoSort(Rng As Range)

    Rng.Resize(, 3).Select

    Rng.Resize(, 3).Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _

    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom


    End Sub
    [/VBA]

    [VBA] Sub MarkDups(Rng As Range)

    Dim cel As Range, c As Range
    Dim firstaddress AsString

    For Each cel In Rng
    If cel.Interior.ColorIndex <> 6 Then
    With Rng
    Set c = .Find(cel, LookIn:=xlValues, After:=Range("A1"))
    IfNot c IsNothingThen
    firstaddress = c.Address
    Do
    If c.Address <> firstaddress Then
    c.Interior.ColorIndex = 6
    EndIf
    Set c = .FindNext(c)
    LoopWhileNot c IsNothingAnd c.Address <> firstaddress
    EndIf
    End With
    EndIf
    Next


    End Sub
    [/VBA]

  3. #3
    VBAX Regular
    Joined
    Mar 2007
    Posts
    47
    Location
    thanx. but i can't use sorting method... i can't sort my data (Order1:=xlAscending), because it's in logical sections...

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How many columns need to be checked for identical-ness?

    Can you post a workbook.
    ____________________________________________
    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

  5. #5
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location
    Just use the "MarkDups" sub and not the other 2 (which will leave out the sort).

  6. #6
    VBAX Regular
    Joined
    Mar 2007
    Posts
    47
    Location
    Quote Originally Posted by xld
    How many columns need to be checked for identical-ness?

    Can you post a workbook.
    i need to check only 1 column.

    no i can't post wbk. it is internal project

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]


    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Const TEST_COL As Long = 1 '<=== and this to match
    Dim i As Long
    Dim iLastRow As Long

    With ActiveSheet

    iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = iLastRow To 1 Step -1
    If Application.CountIf(.Columns(TEST_COL), .Cells(i, TEST_COLUMN).Value) > 1 Then
    .Rows(i).Delete
    End If
    Next i

    End With

    End Sub
    [/vba]
    ____________________________________________
    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

  8. #8
    VBAX Regular
    Joined
    Mar 2007
    Posts
    47
    Location
    Quote Originally Posted by xld
    [vba]


    Public Sub ProcessData()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Const TEST_COL As Long = 1 '<=== and this to match
    Dim i As Long
    Dim iLastRow As Long

    With ActiveSheet

    iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = iLastRow To 1 Step -1
    If Application.CountIf(.Columns(TEST_COL), .Cells(i, TEST_COLUMN).Value) > 1 Then
    .Rows(i).Delete
    End If
    Next i

    End With

    End Sub
    [/vba]
    PERFECT!!!!! THANK YOU!!!!!

Posting Permissions

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