0

The code below is currently deleting all duplicate occurances, including the original, found in column A. I would like to modify the code below to delete all duplicates based on columns A, B, C & D. To clarify, for rows 1 and 2 if columns A match, B match, c match and d match both rows would be deleted. Would anyone be able to assist? I believe an array is needed here, but unsure how. Thanks!

Dim toDel5(), p As Long
Dim RNG5 As Range, Cell5 As Long
Set RNG5 = Range("a1:a4000") 'set your range here

For Cell5 = 1 To RNG5.Cells.Count
    If Application.CountIf(RNG5, RNG5(Cell5)) > 1 Then
        ReDim Preserve toDel5(p)
        toDel5(p) = RNG5(Cell5).Address
        p = p + 1
    End If
Next

On Error GoTo NO_DUPLICATES
For p = UBound(toDel5) To LBound(toDel5) Step -1
    Range(toDel5(p)).EntireRow.Delete

Next p
On Error GoTo 0


End With 
NO_DUPLICATES:
1
  • if you are using xl2007+ then you can use RemoveDuplicates Commented Jun 17, 2013 at 14:58

2 Answers 2

1

This problem seems to require a custom algorithm. Not sure if the aforementioned RemoveDuplicates can deliver a reliable answer for a not-so-simple case, but in this kind of situations I prefer to create something from scratch. As far as your code is not too flexible, I couldn't find the way to propose my correction and thus I have created the whole loop (what I shouldn't do). Note that this code can be adapted to any number of analysed columns/rows. Also bear in mind that it relies on an on-time deletion of the target cells (instead removing the whole row, what can only be done outside the loop); this is just to show you another alternative solution; you can change this code as much as you wish.

Dim maxRow As Long
Dim curStep, startColumn, endColumn As Integer
Dim areDuplicated As Boolean
curStep = 2 'No of rows to be considered
startColumn = 1
endColumn = 4
maxRow = 4000
For curRow = 1 To maxRow - 1
    areDuplicated = True
    For curColumn = startColumn To endColumn
        For curRow2 = curRow + 1 To curRow + curStep - 1
           If (IsEmpty(RNG5.Cells(curRow, curColumn)) Or RNG5.Cells(curRow, curColumn) <> RNG5.Cells(curRow2, curColumn)) Then
              areDuplicated = False
              Exit For
           End If

           If (Not areDuplicated) Then
              Exit For
           End If
        Next
    Next

    If (areDuplicated) Then
        For curRow3 = curRow To curRow + curStep - 1
            For curCol3 = startColumn To endColumn
                RNG5.Cells(curRow3, curCol3).Value = ""
            Next
        Next
    End If
Next
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks Varocarbas, that's a bit simplier than the code I ended up using. The code I used is below in case anyone wanted to see another option. Thanks for the help!

Dim r As Long, c As Long, n As Long, x As Long  
Dim rData As Range 

Application.ScreenUpdating = False 
n = ActiveSheet.Cells(1, 1).CurrentRegion.Columns.Count + 1 
ActiveSheet.Cells(1, n).Value = "TEMP" 

For r = 2 To ActiveSheet.Cells(1, 1).CurrentRegion.Rows.Count 
    ActiveSheet.Cells(r, n).Value = r 
Next r 

Set rData = ActiveSheet.Cells(1, 1).CurrentRegion 

With ActiveSheet.Sort 
    .SortFields.Clear 

    For c = 1 To n
        .SortFields.Add Key:=rData.Cells(1, c).Resize(rData.Rows.Count - 1, 1) 
    Next c 

    .SetRange rData 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
End With 

With rData 
    For r = 2 To .Rows.Count 
         x = 0 
         For c = 1 To n
            If .Cells(r, c).Value <> .Cells(r + 1, c).Value Then 
                x = x + 1 
                Exit For 
            End If 
             Next c 
         If x = 0 Then 
            .Cells(r, n).Value = True 
            .Cells(r + 1, n).Value = True 
        End If     
    Next r 
End With 

With ActiveSheet.Sort 
    .SortFields.Clear 
    .SortFields.Add Key:=rData.Cells(1, n).Resize(rData.Rows.Count - 1, 1) 
    .SetRange rData 
    .Header = xlYes 
    .MatchCase = False 
    .Orientation = xlTopToBottom 
    .SortMethod = xlPinYin 
    .Apply 
End With 

On Error Resume Next 
rData.Columns(n).SpecialCells(xlCellTypeConstants, xlLogical).EntireRow.Delete 
On Error Goto 0 

rData.Columns(n).EntireColumn.Delete 
Application.ScreenUpdating = True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.