Remove duplicate items from a worksheet range

 2012-02-08    Worksheets    3    226

According to the Excel Developer Reference for Office 2007 documentation, or well hidden in the built-in the VBA help file, you should be able to delete duplicate entries from a worksheet range using the RemoveDuplicates method like this:

This method is new in Excel 2007 and will not work in older Excel versions. It takes 2 optional arguments (columns and headers) and should work without any of them, but this seems not to be true. If you use the method and pass values for the 2 arguments it works like intended. Both of the example lines below works just fine:
ActiveSheet.Range("A1:F100").RemoveDuplicates Columns:=Array(1,2,3,4,5,6), Header:=xlYes
ActiveSheet.Range("A1:F100").RemoveDuplicates Array(1,2,3,4,5,6), xlYes
The problem is that sometimes you don't know exactly how many columns you want to check duplicates in, e.g. when you write a procedure for removing duplicates from a worksheet where the number of columns with data may vary from time to time. Then you need to replace Array(1,2,3,4,5,6) in the example above with a variable that contains a similar zero-based Variant array where the upper bound value is one less than the number of columns you want to check for duplicates, e.g. like this:
Dim varItems(0 to 2) As Variant ' 3 columns
Dim varItems(0 to 5) As Variant ' 6 columns
Below is a procedure example that can be used for removing duplicates from any sized range:
Sub RemoveDuplicatesFromRange(objRange As Range, _
    Optional varColumns As Variant = False, _
    Optional blnHasHeader As Boolean = True)
' varColumns should be an array containing column numbers
Dim lngCount As Long, i As Long, j As Long, varItems() As Variant
    If objRange Is Nothing Then Exit Sub
    With objRange
        If Not IsArray(varColumns) Then ' check all columns in the range
            ReDim varItems(0 To .Columns.Count - 1)
            For i = 1 To .Columns.Count
                varItems(i - 1) = i
            Next i
            ReDim varItems(0 To UBound(varColumns) - LBound(varColumns) - 1) ' must be a 0-based variant array
            j = -1
            For i = LBound(varColumns) To UBound(varColumns)
                j = j + 1
                varItems(j) = varColumns(i)
            Next i
        End If
        On Error GoTo FailedToRemoveDuplicates
        If blnHasHeader Then
            .RemoveDuplicates varItems, xlYes
            .RemoveDuplicates varItems, xlNo
        End If
        On Error GoTo 0
    End With
    Exit Sub
    If Application.DisplayAlerts Then
        MsgBox Err.Description, vbInformation, "Error Removing Duplicates From Range: " & objRange.Address
    End If
    Resume Next
End Sub
And below you will find a few examples on how to use the procedure above:
Sub TestRemoveDuplicatesFromRange1()
    RemoveDuplicatesFromRange Range("A1").CurrentRegion ' checks all columns
End Sub

Sub TestRemoveDuplicatesFromRange2()
    RemoveDuplicatesFromRange Range("A1").CurrentRegion, Array(1, 3, 5) ' checks columns 1, 3 and 5
End Sub

Sub TestRemoveDuplicatesFromRange3()
Dim varItems(0 To 2) As Variant ' must be 0-based variant array
    varItems(0) = 1
    varItems(1) = 3
    varItems(2) = 5
    RemoveDuplicatesFromRange Range("A1").CurrentRegion, varItems ' checks columns 1, 3 and 5
End Sub

Sub TestRemoveDuplicatesFromRange4()
Dim lngCount As Long, varItems() As Variant, i As Long, c As Long
    lngCount = Range("A1").CurrentRegion.Columns.Count \ 2
    ReDim varItems(0 To lngCount - 1) ' must be 0-based variant array
    i = 0
    For c = 1 To Range("A1").CurrentRegion.Columns.Count Step 2
        varItems(i) = c
        i = i + 1
    Next c
    RemoveDuplicatesFromRange Range("A1").CurrentRegion, varItems ' checks every odd column
End Sub
It is also possible to manage without the macro RemoveDuplicatesFromRange above by doing something like this:
Sub RemoveDuplicatesFromRangeAlt1()
Dim c As Long, varItems() As Variant ' must be 0-based variant array
    With Range("A1").CurrentRegion
        ReDim varItems(0 To .Columns.Count - 1)
        For c = 1 To .Columns.Count
            varItems(c - 1) = c
        Next c
        .RemoveDuplicates varItems, xlYes ' checks all columns
    End With
End Sub

Sub RemoveDuplicatesFromRangeAlt2()
    Range("A1").CurrentRegion.RemoveDuplicates Array(1, 3, 5), xlYes ' checks columns 1, 3 and 5
End Sub

Sub RemoveDuplicatesFromRangeAlt3()
Dim varItems(0 To 2) As Variant ' must be 0-based variant array
    varItems(0) = 1
    varItems(1) = 3
    varItems(2) = 5
    Range("A1").CurrentRegion.RemoveDuplicates varItems, xlYes ' checks columns 1, 3 and 5
End Sub

Leave a comment:

Your comment will only be published after it has been moderated and found spam free.
Your e-mail address will only be used to display your Gravatar.

Typie34 | 2015-03-03 16:57:29 (GMT)

Thank you very much for this macro. I struggled for hours trying to solve a problem like that. I still don't know what mistake I made, as the part I implemented in my project looks just like yours. Now I found your website, added your macro and it works like a charm.

OPE | 2014-07-07 21:30:31 (GMT)


You were right, something has obviously changed in Excel regarding the RemoveDuplicates method since the example was originally tested and published.
I have updated the RemoveDuplicatesFromRange procedure so it should now work without the error message you reported.

Ludo | 2014-07-07 18:00:44 (GMT)

I did test your code with Excel 2010 and it always return an Error Removing duplicates msg box.
the msg = Invalid Procedure call or Invalid Argument.
The range has 6 columns with headers and 1172 rows.
Any idea what's wrong?


any help welcome.
Best regards,