发布网友 发布时间:2022-02-25 05:24
共1个回答
热心网友 时间:2022-02-25 06:53
'按下
Alt+F11
组合键,
输入并运行(按F5键即可运行)下列代码:
'本示例在指定工作表的列中对数据进行排序,然后删除包含重复数据的行。
Sub
DeleteColumnDupes()
Dim
strSheetName
As
String,
strColumnLetter
As
String
strSheetName
=
"Sheet1"
'
删除工作表中的重复行
strColumnLetter
=
"A"
'
以
A
列中的重复项作为删除条件
Dim
strColumnRange
As
String
Dim
rngCurrentCell
As
Range
Dim
rngNextCell
As
Range
strColumnRange
=
strColumnLetter
&
"1"
Worksheets(strSheetName).Range(strColumnRange).Sort
_
Key1:=Worksheets(strSheetName).Range(strColumnRange)
Set
rngCurrentCell
=
Worksheets(strSheetName).Range(strColumnRange)
Do
While
Not
IsEmpty(rngCurrentCell)
Set
rngNextCell
=
rngCurrentCell.Offset(1,
0)
If
rngNextCell.Value
=
rngCurrentCell.Value
Then
rngCurrentCell.EntireRow.Delete
End
If
Set
rngCurrentCell
=
rngNextCell
Loop
End
Sub