vba 从excel vba中的单元格范围中删除重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31631231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:49:05  来源:igfitidea点击:

Remove Duplicates from range of cells in excel vba

excelvbaexcel-vbaexcel-2013

提问by Hideandseek

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'.

我正在尝试删除 excel 2013 VBA 中的重复项。但我收到错误“对象不支持此属性或方法”。问题是我没有静态范围可供选择。我想从列标题“abcd”中删除重复项。

Cells.Find(what:="abcd").Activate
ActiveCell.EntireColumn.Select
Set rng = Selection
ActiveSheet.rng.RemoveDuplicates

回答by

You need to tell the Range.RemoveDuplicates methodwhat column to use. Additionally, since you have expressed that you have a header row, you should tell the .RemoveDuplicates method that.

您需要告诉Range.RemoveDuplicates 方法使用哪个列。此外,由于您已经表示您有一个标题行,您应该告诉 .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

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.

您的原始代码似乎想从单个列中删除重复项,同时忽略周围的数据。这种情况是非典型的,我已经包含了周围的数据,以便 .RemoveDuplicates 过程不会扰乱您的数据。如果您真的想将 RemoveDuplicates 过程隔离到单个列,请回发评论。

回答by mahi_0707

To remove duplicates from a single column

从单个列中删除重复项

 Sub removeDuplicate()
 'removeDuplicate Macro
 Columns("A:A").Select
 ActiveSheet.Range("$A:$A7").RemoveDuplicates Columns:=Array(1), _ 
 Header:=xlNo 
 Range("A1").Select
 End Sub

if you have header then use Header:=xlYes

如果你有标题然后使用 Header:=xlYes

Increase your range as per your requirement.
you can make it to 1000 like this :

根据您的要求增加您的范围。
你可以像这样达到 1000:

ActiveSheet.Range("$A$1:$A$1000")

ActiveSheet.Range("$A$1:$A$1000")

More info here here

更多信息在这里