简体   繁体   中英

Remove Duplicates in VBA excel

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM