我想比较A列和B列,并清除B列中的重复项。
Sub deleteDoublicates()
Dim i As Long
With Sheets("Tabelle1")
For i = .Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
If Application.IsNumber(Application.Match(.Cells(i, "A"), Sheets("Tabelle1").Columns(1), 0)) Then
.Cells(i, "B").ClearContents
End If
Next
End With
End Sub
必定存在完全错误,因为它会删除所有内容。
答案 0 :(得分:1)
您可以使用变体从application.match中捕获错误。
Sub deleteDoublicates()
Dim i As Long, m as variant
With Sheets("Tabelle1")
For i = .Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
m = Application.Match(.Cells(i, "A"), Sheets("Tabelle1").Columns(1), 0)
if not iserror(m) then
.Cells(m, "B").ClearContents
End If
Next i
End With
End Sub
您自己的代码正在迭代i到A列中的行,但是莫名其妙地使用该行号来清除B列。