RemoveDuplicates Columns Parameter with Variable

trough

Board Regular
Joined
Oct 26, 2010
Messages
55
How can I pass a variable to the Columns parameter of the RemoveDuplicates method?

'Option Base 1
'Dim Col2Rmv() As Long
'Col2Rmv(1)=1
'Col2Rmv(2)=3

These all work but they all involve hardcoding the Columns parameter:

'Range("A1:G10").RemoveDuplicates Columns:=1 'consider only one column
'Range("A1:G10").RemoveDuplicates Columns:=VBA.Array(1) 'consider only one column
'Range("A1:G10").RemoveDuplicates Columns:=VBA.Array(1,3,7) 'consider multiple columns simultaneously
'Range("A1:G10").RemoveDuplicates Columns:=VBA.Array(Col2Rmv(1), Col2Rmv(2)) 'consider multiple columns

How can I pass the array Col2Rmv directly to the Columns parameter such as this (but it doesn't work):

'Range("A1:G10").RemoveDuplicates Columns:=Col2Rmv

Col2Rmv might be this:
'Col2Rmv(1)=1
'Col2Rmv(2)=3
or:
'Col2Rmv(1)=1
'Col2Rmv(2)=3
'Col2Rmv(3)=7
or some other list of column numbers not predetermined.

I've tried several different things but none have worked properly to where all columns of interest are considered simultaneously.

Is there a way to create a loop and add individual values to an Array and then pass that to the Columns parameter?

What I've tried:
'Dim Col2Rmv() As Variant

populate Col2Rmv with column numbers of interest

'Range("A1:G10").RemoveDuplicates Columns:=Col2Rmv
'Range("A1:G10").RemoveDuplicates Columns:=(Col2Rmv)
'Range("A1:G10").RemoveDuplicates Columns:=Evaluate(Col2Rmv)
'Range("A1:G10").RemoveDuplicates Columns:=Col2Rmv(1)
'Range("A1:G10").RemoveDuplicates Col2Rmv
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You haven't said how you intent selecting the variable columns for the remove duplicates function. Anyhow, here's 3 options - just uncomment the particular case you're using, or let us know if you intend selecting the columns a different way.

VBA Code:
Option Explicit
Sub Variable_Columns_3_Options()
    Dim temp, cols, i As Long
   
'    1. If you put the columns into the code uncomment this
'    temp = Split("1,2,4", ",")
'
'    2. If you get the column numbers from a particular cell uncomment this
'    temp = Split(Range("F1"), ",")
'
'    3. If you get the columns from a range of cells (example) uncomment this
'    Dim c As Range, s As String
'    For Each c In Range("G1:I1")
'        s = s & "," & c
'    Next c
'    s = Right(s, Len(s) - 1)
'    temp = Split(s, ",")
       
    'In all cases use this
    ReDim cols(0 To UBound(temp))
    For i = 0 To UBound(cols)
        cols(i) = temp(i)
    Next i
    Range("A1").CurrentRegion.RemoveDuplicates Columns:=(cols), Header:=xlYes

End Sub

Example using option 2
Before:
Book1
ABCDEFGHI
1HDR1HDR2HDR3HDR41,2,4124
2a1x1
3a2x1
4a1x1
5b1x1
6b1x1
7c1x1
Sheet1


After:
Book1
ABCDEFGHI
1HDR1HDR2HDR3HDR41,2,4124
2a1x1
3a2x1
4b1x1
5c1x1
6
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,125
Messages
6,123,193
Members
449,090
Latest member
bes000

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