I try to sort a sheet in my workbook. After the macro sorted my table it should remove all duplicates based on the column A.
But every time I use the macro, I get the following error:
Sub SortAndRemoveDUBS()
Dim Rng As Range
Dim LastRow As Long
Dim i As Long
Application.ScreenUpdating = False
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = Range("A4:P" & LastRow)
With Rng
.Sort Key1:=Range("A4"), Order1:=xlAscending, key2:=Range("P4"), order2:=xlDescending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End With
Dim arr() As Variant
Dim cnt As Long
cnt = 0
For i = LastRow To 2 Step -1
If WorksheetFunction.CountIf(Range(Cells(2, "A"), Cells(i, "A")), Cells(i, "A")) > 1 Then
ReDim Preserve arr(cnt)
arr(cnt) = i
cnt = cnt + 1
End If
Next i
If Len(Join(arr)) > 0 Then ActiveSheet.Range("A" & Join(arr, ",A")).EntireRow.Delete
Application.ScreenUpdating = True
End Sub
This line gets highlighted:
ActiveSheet.Range("A" & Join(arr, ",A")).EntireRow.Delete
Does someone see what the probleme is?