删除excel vba中单元格范围的重复 [英] Remove Duplicates from range of cells in excel vba
问题描述
我正在尝试删除excel 2013 VBA中的重复项。但是我收到错误对象不支持此属性或方法。问题是我没有静态范围来选择。我想从列heaader'abcd'删除重复。
I'm trying to remove duplicates in excel 2013 VBA. but I'm getting error "object does not support this property or method". The problem is I don't have static range to select. I want remove duplicates from the column heaader 'abcd'.
Cells.Find(what:="abcd").Activate
ActiveCell.EntireColumn.Select
Set rng = Selection
ActiveSheet.rng.RemoveDuplicates
推荐答案
p>您需要告知 Range.RemoveDuplicates方法要使用的列。另外,由于你表示你有一个标题行,你应该告诉.RemoveDuplicates方法。
You need to tell the Range.RemoveDuplicates method what column to use. Additionally, since you have expressed that you have a header row, you should tell the .RemoveDuplicates method that.
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
您的原始代码似乎想要从单个列中删除重复项,而忽略周围的数据。这种情况是非典型的,我已经包含了周围的数据,以便.RemoveDuplicate进程不会扰乱你的数据。如果您真的想将RemoveDuplicate进程隔离到单个列,请发回评论。
Your original code seemed to want to remove duplicates from a single column while ignoring surrounding data. That scenario is atypical and I've included the surrounding data so that the .RemoveDuplicates process does not scramble your data. Post back a comment if you truly wanted to isolate the RemoveDuplicates process to a single column.
这篇关于删除excel vba中单元格范围的重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!