Consulting

Results 1 to 5 of 5

Thread: removing duplicates

  1. #1

    removing duplicates

    I am trying to delete a row if the value in one cell is a duplicate of the one above it. I can't compare the entire row because the values in the other cells may or may not be the same as the row above.

    what I have right now works, but boy is is SLOW - and all sorts of blinks going on! Wondered if anyone has a suggestion for making it more efficient:

    Dim TotalRows As Integer
    Dim Row As Integer

    TotalRows = ActiveSheet.UsedRange.Rows.Count
    For Row = TotalRows To 2 Step -1
    If Cells(Row, 3).Value = Cells(Row - 1, 3).Value Then
    Rows(Row).Delete
    End If
    Next Row
    End Sub

  2. #2
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    [vba]
    application.screenupdating = false

    Dim TotalRows As Integer
    Dim Row As Integer

    TotalRows = ActiveSheet.UsedRange.Rows.Count
    For Row = TotalRows To 2 Step -1
    If Cells(Row, 3).Value = Cells(Row - 1, 3).Value Then
    Rows(Row).Delete
    End If
    Next Row

    application.screenupdating = true

    [/vba] that should get rid of the blinks and make things a little quicker

    EDIT - also, check out the article 'optimize your code' available here

  3. #3
    a little quicker? Tons! Thanks so much.

    Could you enlighten me as to why those statements helped so much, please?

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Try this one for column C:
    [VBA]Sub RemoveDuplicates()
    Worksheets("Sheet1").Range("c1").Sort _
    key1:=Worksheets("Sheet1").Range("c1")
    Set currentCell = Worksheets("Sheet1").Range("c1")
    Do While Not IsEmpty(currentCell)
    Set nextCell = currentCell.Offset(1, 0)
    If nextCell.Value = currentCell.Value Then
    currentCell.EntireRow.Delete
    End If
    Set currentCell = nextCell
    Loop
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    Quote Originally Posted by alliejane
    Could you enlighten me as to why those statements helped so much, please?
    not to be evasive, but it's in the article, and johnske explains it much better than me simply saying 'it turns off screen updating'

    you should really read that article 'cover to cover'; i didnt read it until after I developed a few things and wish I had learnt it sooner.

Posting Permissions

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