2016-07-12 20 views

Antwort

0

Nun, funktioniert dieser Code perfekt

ActiveSheet.Range("$A$1:$B$4").RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo 
+0

aber wählt die ganze Spalte. Ich habe viele Spalten, möchte nur Duplikate basierend auf 1 Spalte löschen –

+0

Setzen Sie einfach anstelle von [column_number] Ihre Spalte und anstelle von [data_range] den Tabellenbereich 'ActiveSheet.Range ([data_range]). RemoveDuplicates Columns: = [ column_number], Header: = xlNo' – lancer102rus

+0

Nein, scheint nicht zu funktionieren. Habe alle Varianten davon probiert –

0

Vielleicht nicht die optimale Lösung, aber es macht die Arbeit

Public Sub DeleteDuplicates() 
    Dim rng As Range, cell As Range, find As Range, previous As Range 
    Set rng = Worksheets("Test").Range("A:A") 

    ' For each cell of the column 
    For Each cell In rng 
     ' Check duplicates only if the cell is not empty 
     If Trim(cell) <> "" Then 
      ' Search for the same value in column A 
      Set find = rng.find(What:=cell, After:=cell, LookAt:=xlWhole, MatchCase:=True) 
      ' if a result is found 
      If Not find Is Nothing Then 
       Do 
        Set previous = Nothing 
        ' If the column B is the same, save the row as "to be deleted" 
        ' The delete is not now otherwise the FindNext won't work 
        If find.Offset(, 1) = cell.Offset(, 1) Then 
         Set previous = find 
        End If 

        Set find = rng.FindNext(find) 

        ' If a duplicate was found, delete the raw 
        If Not previous Is Nothing Then 
         previous.EntireRow.Delete 
        End If 

        ' if no more duplicate, exit the do loop 
        If find Is Nothing Then 
         Exit Do 
        End If 

        ' if address of the result is the same as the clel, exit the do loop 
        If cell.Address = find.Address Then 
         Exit Do 
        End If 
       Loop While True 
      End If 
     End If 
    Next 
End Sub 
+0

Hallo. Es wird ein Fehler 91 ausgeführt. –

Verwandte Themen