+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    05-07-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    15

    Clear Duplicates

    Hey everyone this is my first post on any excel forums. I can usually find a solution just by browsing but not this time. What i'm trying to do sounds simple enough. I have 2 Columns of data, example;

    1-Apr 1423
    1-Apr 1426
    24-Apr 1430
    8-May 1454
    8-May 1460
    15-May 1500
    15-Jun 1525
    15-Jun 1555
    15-Jun 1556

    I want to clear (not delete) the cells with a duplicate date in them while keeping the cell with the number next to it. So in this case they would be A2, A5, A8, A9. I've come up with a code that looks pretty logical but it doesn't work. Here's what it looks like.

    Code:
    Public Sub ClearDupes()
    
        Dim iRows As Integer
        Dim i As Integer
        Dim lCount As Integer
        
        lCount = 1
        iRows = ActiveSheet.UsedRange.Rows.Count
        iRows = iRows - 1
       Sheets("Temp").Range("A1").Activate
        
    Do Until IsEmpty(ActiveCell)
        For i = 1 To iRows
            If ActiveCell Is ActiveCell.Offset(i, 0) Then
                ActiveCell.Offset(i, 0).Clear
                lCount = lCount + 1
                Else:
            End If
        Next i
    
        ActiveCell.Offset(lCount, 0).Select
        lCount = 1
    Loop
    
    End Sub
    Nothing much happens when i run this, it goes to the empty cell under the last used cell and doesn't clear anything.

    TIA

    -Chris

    --Edited a few bits of the code.
    Last edited by Falantar; 05-07-2009 at 01:02 PM. Reason: Solved

  2. #2
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,131

    re: Clear Duplicates

    Welcome to the forum.

    Try this:
    Code:
    Public Sub ClearDupes()
        Dim iRow    As Long
    
        With Worksheets("Temp")
            For iRow = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1
                With .Cells(iRow, "A")
                    If .Value = .Offset(-1).Value Then .ClearContents
                End With
            Next iRow
        End With
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    05-07-2009
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Clear Duplicates

    Wow, worked like a charm. Thanks for the quick reply!

  4. #4
    Valued Forum Contributor Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    2010
    Posts
    942

    Re: Clear Duplicates

    SHG's solution is great as usual. It would need a slight tweak if your goal was to stop at the first blank cell.

    Here is my approach from the bottom down to first blank cell. Of course it would need a slight tweak if your goal was to process all the cells in column A (bottom up approach).
    Code:
    Sub ClearDupsInA()
      Dim c As Range, val As Long
      With Sheets("Temp")
        val = .Range("A1").Value
        For Each c In .Range("A2", .Range("A2").End(xlDown))
            If Not (IsEmpty(c.Offset(-1, 0))) Then val = c.Offset(-1, 0).Value
            If c.Value = val Then
              c.ClearContents
              Else: val = c.Value
            End If
        Next c
      End With
    End Sub

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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.2.0