从 excel vba 中的单元格范围中删除重复项
Remove Duplicates from range of cells in excel vba
我正在尝试删除 excel 2013 VBA 中的重复项。但我收到错误 "object does not support this property or method"。问题是我没有 select 的静态范围。我想从列标题 'abcd' 中删除重复项。
Cells.Find(what:="abcd").Activate
ActiveCell.EntireColumn.Select
Set rng = Selection
ActiveSheet.rng.RemoveDuplicates
您需要告诉 Range.RemoveDuplicates method 要使用的列。此外,由于您已经表示您有一个 header 行,因此您应该告诉 .RemoveDuplicates 方法。
Sub dedupe_abcd()
Dim icol As Long
With Sheets("Sheet1") '<-set this worksheet reference properly!
icol = Application.Match("abcd", .Rows(1), 0)
With .Cells(1, 1).CurrentRegion
.RemoveDuplicates Columns:=icol, Header:=xlYes
End With
End With
End Sub
您的原始代码似乎想要从单个列中删除重复项,同时忽略周围的数据。这种情况是非典型的,我已经包含了周围的数据,这样 .RemoveDuplicates 过程就不会扰乱您的数据。 Post 如果您真的想将 RemoveDuplicates 过程隔离到单个列,请回复评论。
从单个列中删除重复项
Sub removeDuplicate()
'removeDuplicate Macro
Columns("A:A").Select
ActiveSheet.Range("$A:$A7").RemoveDuplicates Columns:=Array(1), _
Header:=xlNo
Range("A1").Select
End Sub
如果你有 header 然后使用 Header:=xlYes
根据您的要求增加范围。
你可以像这样达到 1000 :
ActiveSheet.Range("$A:$A00")
这里有更多信息here
如果范围内只有一列要清理,只需在末尾添加“(1)”即可。它指示范围 Excel 中的哪一列将删除重复项。
类似于:
Sub norepeat()
Range("C8:C16").RemoveDuplicates (1)
End Sub
此致
我正在尝试删除 excel 2013 VBA 中的重复项。但我收到错误 "object does not support this property or method"。问题是我没有 select 的静态范围。我想从列标题 'abcd' 中删除重复项。
Cells.Find(what:="abcd").Activate
ActiveCell.EntireColumn.Select
Set rng = Selection
ActiveSheet.rng.RemoveDuplicates
您需要告诉 Range.RemoveDuplicates method 要使用的列。此外,由于您已经表示您有一个 header 行,因此您应该告诉 .RemoveDuplicates 方法。
Sub dedupe_abcd()
Dim icol As Long
With Sheets("Sheet1") '<-set this worksheet reference properly!
icol = Application.Match("abcd", .Rows(1), 0)
With .Cells(1, 1).CurrentRegion
.RemoveDuplicates Columns:=icol, Header:=xlYes
End With
End With
End Sub
您的原始代码似乎想要从单个列中删除重复项,同时忽略周围的数据。这种情况是非典型的,我已经包含了周围的数据,这样 .RemoveDuplicates 过程就不会扰乱您的数据。 Post 如果您真的想将 RemoveDuplicates 过程隔离到单个列,请回复评论。
从单个列中删除重复项
Sub removeDuplicate()
'removeDuplicate Macro
Columns("A:A").Select
ActiveSheet.Range("$A:$A7").RemoveDuplicates Columns:=Array(1), _
Header:=xlNo
Range("A1").Select
End Sub
如果你有 header 然后使用 Header:=xlYes
根据您的要求增加范围。
你可以像这样达到 1000 :
ActiveSheet.Range("$A:$A00")
这里有更多信息here
如果范围内只有一列要清理,只需在末尾添加“(1)”即可。它指示范围 Excel 中的哪一列将删除重复项。 类似于:
Sub norepeat()
Range("C8:C16").RemoveDuplicates (1)
End Sub
此致