+ Reply to Thread
Results 1 to 2 of 2

Compare cells and delete

Hybrid View

  1. #1
    Registered User
    Join Date
    05-27-2010
    Location
    Norway
    MS-Off Ver
    Excel 2003
    Posts
    1

    Compare cells and delete

    Hi,
    I have a worksheet with many rows and in column D I have different values/text in each cell (no blanks). The first 20 rows have a certain value/text and the next 34 have another certain value/text and so on and so on. (i.e. 20 ab's and 34 qx's)
    What I want is to only keep one of the lines for each value/text.

    My thought was to compare the cell D1 with D2 and if they were the same then delete entire row of D2 and then continue with D1 and D3 and so on until the end and then continue with comparing D2 and D3...... The script I wrote was:
    Sub Makro4()
    a = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 1 To a
    b = i + 1
    For j = b To a
    If Range("D" & i).Value = Range("D" & j).Value Then
    Range("D" & j).EntireRow.Delete
    Else
    a = Cells(Rows.Count, 1).End(xlUp).Row
    End If
    Next
    Next
    End Sub
    But this script leaves 5 of each line, and I don't understand why.
    Can anyone help?

    If you have a better way of solving this it will be greatly appreciated. (Total number of rows is 5000)
    W
    Last edited by Leith Ross; 05-27-2010 at 07:50 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Compare cells and delete

    Hello whitebalance,

    There a several methods you can use to do this. This macro uses the Dictionary object to store only the unique entries. Any duplicates will have the entire row cleared. Afterward, the remaining data is sorted in ascending order to remove the blank rows.
    Sub DeleteRepeats()
    
      Dim Cell As Range
      Dim DSO As Object
      Dim Key As String
      Dim Rng As Range
      Dim RngEnd As Range
      
        Set Rng = Range("D1")
        Set RngEnd = Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Range(Rng, RngEnd))
        
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
          For Each Cell In Rng
            Key = Trim(Cell.Text)
            If Not DSO.Exists(Key) Then
               DSO.Add Key, 1
            Else
               Cell.EntireRow.ClearContents
            End If
          Next Cell
          
        Rng.Sort Key1:=Rng.Cells(1, 1), Order1:=xlAscending, Header:=xlGuess, _
                 MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
        
        Set DSO = Nothing
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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