删除 VBA excel 中的重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26511155/
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-12 04:56:53  来源:igfitidea点击:

Remove Duplicates in VBA excel

excelvbaexcel-vba

提问by user4170051

I am trying to remove the duplicates at the end of a macro I wrote in vba. The macro runs but when it comes to the point to remove the duplicates it says that the object doesn't support the property or method when the object is Dimed as a range and set as a range as well. I am really confused on why this happening and I can't seem to spot what is causing this error. I have pasted the code below which causes the error and the point where I set the range object. Any help would be greatly appreciated.

我正在尝试删除我在 vba 中编写的宏末尾的重复项。宏运行但是当涉及到删除重复项时,它表示当对象被 Dimed 作为一个范围并设置为一个范围时,该对象不支持该属性或方法。我真的很困惑为什么会发生这种情况,我似乎无法发现导致此错误的原因。我在下面粘贴了导致错误的代码以及我设置范围对象的点。任何帮助将不胜感激。

Set WS = ThisWorkbook.ActiveSheet
    With WS
        Set Rng1 = .Range("B2:B" & .Range("B" & .Rows.Count).End(xlUp).Row)
        Set rng2 = .Range("C1:D" & .Range("C" & .Rows.Count).End(xlUp).Row)
    End With

If UBound(WrdArray2) < 0 Then
    ActiveSheet.rng2.RemoveDuplicates
        End
End If

回答by Paresh J

Instead of

代替

ActiveSheet.rng2.RemoveDuplicates

Just try the below:

只需尝试以下操作:

rng2.RemoveDuplicates

What causing the error is you have already set range for object rng2 and rng2 does not include in Activesheet. That is, rng2 is separate object created by you and is not the property of Activesheet.

导致错误的原因是您已经为对象 rng2 设置了范围,而 rng2 未包含在 Activesheet 中。也就是说,rng2 是您创建的单独对象,而不是 Activesheet 的属性。