【发布时间】:2017-03-20 04:10:55
【问题描述】:
我正在尝试删除单个列中重复单元格的内容。我想保留条目的第一次出现,但删除它下面的所有重复项。
我只能找到删除整行而不清除内容的代码。
Sub Duplicate()
With Application
' Turn off screen updating to increase performance
.ScreenUpdating = False
Dim LastColumn As Integer
LastColumn = Cells.Find(What:="*", After:=Range("U1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
With Range("U1:U" & Cells(Rows.Count, 1).End(xlUp).Row)
' Use AdvanceFilter to filter unique values
.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
.SpecialCells(xlCellTypeVisible).Offset(0, LastColumn - 1).Value = 1
On Error Resume Next
ActiveSheet.ShowAllData
'Delete the blank rows
Columns(LastColumn).SpecialCells(xlCellTypeBlanks).Cells.Clear
Err.Clear
End With
Columns(LastColumn).Clear
.ScreenUpdating = True
End With
End Sub
【问题讨论】:
-
只需使用显示的任何算法来检测重复单元格,然后使用
cells.clear方法而不是使用entirerow.delete方法。如果它不起作用,请发布您的代码。 -
@RonRosenfeld 我只能找到删除所有条目并且不保留第一次出现的代码。我编辑了我的帖子以显示我正在尝试使用的代码。
-
我发布了一些你可以使用的东西,它使用了不同的算法。高级过滤器似乎不适合您的目的。