In einem Tabellenblatt stehen in unterschiedlichen Zellen, ein oder auch mehrere Wörter.
Ich möchte das mittels eines Makro alle doppelten Wörter in dem Tabellenblatt gelöscht werden.
Kann mir jemand den Code dafür nennen?
Danke und Gruß
Michael
Sub KeineDoppelten()
Dim AllCells As Range, Cell As Range
Dim OhneDuplikate As New Collection
Der auf Duplikate zu scannende Bereich
Set AllCells = Range("A1:A65536") wenn deine Daten in Spalte A stehen
Damit er nicht bei einer Doublette aufhört
On Error Resume Next
For Each Cell In AllCells
OhneDuplikate.Add Cell.Value, CStr(Cell.Value)
Bemerkung: 2. Argument für Add-Methode muss vom Typ string sein
Next Cell
Einstellen der Werte ohne Duplikate in Spalte D
Range("D1").Activate
j = 0
For i = 1 To OhneDuplikate.Count
ActiveCell.Offset(j, 0).Value = OhneDuplikate(i)
j = j + 1
Next
End Sub
Oder von JensF
Sub DopplerLöschen2Dim()
Dim LastC As Long, X As Long
LastC = Range("a65536").End(xlUp).Row
ReDim Liste(1 To LastC)
For X = 1 To LastC
Liste(X) = Cells(X, 1) & Cells(X, 2) & Cells(X, 3)
Next
For X = LastC To 1 Step 1
If WorksheetFunction.Match(Cells(X, 1) & Cells(X, 2) & Cells(X, 3), Liste, 0)
Oder von JensF Doppelte in Spalte gesamte Zeile Löschen
Sub doppelteLöschen()
Dim LastC As Long, x As Long
LastC = Range("a65536").End(xlUp).Row
For x = LastC To 1 Step -1
If WorksheetFunction.CountIf(Range("a1:a" & x), Cells(x, 1)) > 1 Then
Cells(x, 1).EntireRow.Delete
End If
Next
End Sub
' Paul Stefan Paul Markus Paul Stefan Markus Paul Paul
Public Sub RemoveDuplicatePartsInCell(ByVal targetCell As Range)
Dim cellTextParts
' den Text in ein array an den spaces splitten
cellTextParts = VBA.Split(VBA.Trim(targetCell.Value), " ")
' keine trennzeichen, keine sub-texte vorhanden, exit
If (VBA.IsArray(cellTextParts) = False) Then
Exit Sub
End If
' das array duchgehen, alle mit allen vergleichen
Dim i, j, result
' die doppelte string auf "" setzen
For i = LBound(cellTextParts) To UBound(cellTextParts)
If (cellTextParts(i) "") Then
For j = i + 1 To UBound(cellTextParts)
If (VBA.Trim(cellTextParts(i)) = VBA.Trim(cellTextParts(j))) Then
cellTextParts(j) = ""
End If
Next j
End If
Next i
' den gesammten text wiederherstellen
result = ""
For i = LBound(cellTextParts) To UBound(cellTextParts)
If (cellTextParts(i) "") Then
If (result = "") Then
result = cellTextParts(i)
Else
result = result & " " & cellTextParts(i)
End If
End If
Next i
' den text ohne doppelte sub-texte zurueck in die Zelle schreiben
targetCell.Value = result
End Sub
Public Sub main()
Call RemoveDuplicateEntriesInUsedRange
Call RemoveDuplicatePartsInUsedRangeCells
End Sub
' In this sub all the cells in the used range on the active sheet
' are tested for their duplicates.
' Only first one occurance of each different cell-value is left in the used range,
' all other occurances are deleted. So after the sub finishes, each cell-value
' in the used range will be unique.
' Method used : compare each cell value with all other values.
' Each cell, which had some duplicities will be marked with a blue color.
' Each cell, where the value was deleted due to its duplicity will be makked with red.
Public Sub RemoveDuplicateEntriesInUsedRange()
On Error GoTo Err_RemoveDuplicateEntries
Dim cellOuther As Range
Dim cellInner As Range
Dim cellsInUsedRangeOnActiveSheet As Range
Application.ScreenUpdating = False
Set cellsInUsedRangeOnActiveSheet = ActiveSheet.UsedRange.Cells
For Each cellOuther In cellsInUsedRangeOnActiveSheet
' do not compare empty cells
If (cellOuther.Value = "") Then
GoTo continue_outer
End If
For Each cellInner In cellsInUsedRangeOnActiveSheet
' do not compare empty cells
' do not compare the cell with it self
If (cellInner.Value = "" Or cellInner.Row = cellOuther.Row And cellInner.Column = _
cellOuther.Column) Then
GoTo continue_inner
End If
If (VBA.Trim(cellOuther.Value) = VBA.Trim(cellInner.Value)) Then
cellInner.Value = "" ' delete the duplicate value
cellInner.Interior.Color = VBA.RGB(255, 0, 0) ' mark the cell where the value _
was deleted with red
cellOuther.Interior.Color = VBA.RGB(0, 0, 255) ' mark the cell which had some _
duplicities with blue
End If
continue_inner:
Next cellInner
continue_outer:
Next cellOuther
Application.ScreenUpdating = True
Exit Sub
Err_RemoveDuplicateEntries:
Application.ScreenUpdating = True
VBA.MsgBox Err.Description & "[" & Err.Number & "]", vbCritical, "Error in ' _
RemoveDuplicateEntries'"
End Sub
' Paul Stefan Paul Markus Paul Stefan Markus Paul Paul
Public Sub RemoveDuplicatePartsInUsedRangeCells()
On Error GoTo Err_RemoveDuplicatePartsInUsedRangeCells
Dim targetCell, cellTextParts
Dim i, j, result
' fuer alle zellen im used range
For Each targetCell In ActiveSheet.UsedRange.Cells
' den Text in ein array an den spaces splitten
cellTextParts = VBA.Split(VBA.Trim(targetCell.Value), " ")
' keine trennzeichen, keine sub-texte vorhanden, continue
If (VBA.IsArray(cellTextParts) = False) Then
GoTo continue_targetCell
End If
' das array duchgehen, alle mit allen vergleichen, die doppelten strings auf "" setzen
For i = LBound(cellTextParts) To UBound(cellTextParts)
If (cellTextParts(i) "") Then
For j = i + 1 To UBound(cellTextParts)
If (VBA.Trim(cellTextParts(i)) = VBA.Trim(cellTextParts(j))) Then
cellTextParts(j) = ""
End If
Next j
End If
Next i
' den gesammten text wiederherstellen
result = ""
For i = LBound(cellTextParts) To UBound(cellTextParts)
If (cellTextParts(i) "") Then
If (result = "") Then
result = cellTextParts(i)
Else
result = result & " " & cellTextParts(i)
End If
End If
Next i
' den text ohne doppelte sub-texte zurueck in die Zelle schreiben
targetCell.Value = result
continue_targetCell:
Next targetCell
Exit Sub
Err_RemoveDuplicatePartsInUsedRangeCells:
VBA.MsgBox Err.Description & "[" & Err.Number & "]", vbCritical, "Error in ' _
RemoveDuplicatePartsInUsedRangeCells'"
End Sub
===========================================================================
Gruss Dan, cz