Remove Duplicates Multiple Columns

trough

Board Regular
Joined
Oct 26, 2010
Messages
55
I cannot figure out why I am getting a "Subscript out of range" error with this line:

WS.Range(Cells(1, 1), Cells(5, 3)).RemoveDuplicates Columns:=Array(2, 3), Header:=xlNo
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
More:

This works:
Range(Cells(1, 1), Cells(5, 3)).RemoveDuplicates Columns:=2, Header:=xlNo

But this doesn't:
Range(Cells(1, 1), Cells(5, 3)).RemoveDuplicates Columns:=Array(2), Header:=xlNo

Why?
 
Upvote 0
why?
because of this

try like this bro
VBA Code:
Sub www()

Dim ws As Worksheet
Dim ar As Variant
Set ws = ThisWorkbook.Sheets("Sheet2")

ar = [{1,2}]

Range(Cells(1, 1), Cells(5, 3)).RemoveDuplicates ar(1), Header:=xlNo

End Sub
 
Upvote 0
Solution
But how would I build a string of multiple columns, Ex. MyStr="1,3" -> ar="[{" & MyStr & "}]", that would be accepted by the RemoveDuplicates command? It doesn't seem to like a string.
 
Upvote 0
This code will fail if the sheet is not the active sheet
VBA Code:
WS.Range(Cells(1, 1), Cells(5, 3)).RemoveDuplicates Columns:=Array(2, 3), Header:=xlNo
it should be
VBA Code:
WS.Range(WS.Cells(1, 1), WS.Cells(5, 3)).RemoveDuplicates Columns:=Array(2, 3), Header:=xlNo
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top