VBA Inserting an array as columns to remove in excel table

CRHAAG

New Member
Joined
Jul 3, 2014
Messages
46
I have an excel table (reportInfo(0)).ListObjects(reportInfo(1)) that I am trying to remove duplicates from. To do this, I need to call out an array with the columns with duplicate values. In this case, I need to remove any rows in which columns 1 and 3 are the same. My code is as follows:

VBA Code:
Dim columnsToDuplicate As Variant
columnsToDuplicate = Array(1, 3)

'REMOVE DUPLICATES
table.Range.RemoveDuplicates Columns:=Array(columnsToDuplicate), Header:=xlYes

When I run it this way, I get a type mismatch error. If I change the remove duplicates code to either of the following, it works:

VBA Code:
'REMOVE DUPLICATES
table.Range.RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes

Or:

VBA Code:
'REMOVE DUPLICATES
table.Range.RemoveDuplicates Columns:=Array(columnsToDuplicate(0), columnsToDuplicate(1) ), Header:=xlYes

I can't use the second option because the number of columns that I want to use to remove duplicates will not always be the same.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
This is redundant.
Array(columnsToDuplicate)

columnsToDuplicate is already an array. It would be equivalent to saying Array(Array(1,3))

Try this...
VBA Code:
'REMOVE DUPLICATES
table.Range.RemoveDuplicates Columns:=columnsToDuplicate, Header:=xlYes
 
Upvote 0
This is redundant.
Array(columnsToDuplicate)

columnsToDuplicate is already an array. It would be equivalent to saying Array(Array(1,3))

Try this...
VBA Code:
'REMOVE DUPLICATES
table.Range.RemoveDuplicates Columns:=columnsToDuplicate, Header:=xlYes
Thanks, but I think I tried that one already. I still get "Invalid procedure call or argument" on that line when I write it that way.
 
Upvote 0
Found it, for some reason columnsToDuplicate needs to be inside ()
table.Range.RemoveDuplicates Columns:=(columnsToDuplicate), Header:=xlYes
 
Upvote 0
I have been able to duplicate what the OP has stated. I do not know why what AlphFrog posted does not work. :confused:
.. at least for me and the OP.

@AlphaFrog
Have you been able to actually run what you posted?
 
Upvote 0
No Peter. I only have Excel 2003 which doesn't support the .RemoveDuplicates method.

I don't understand why the parentheses are needed as well.
OK, thanks. Yes baffling to me. :eek:
 
Upvote 0
RoryA has provided some more information, though we still do not know why AlphaFrog's code from post #2 does not work.
Here are two other methods, as suggested by RoryA, that do also work, though perhaps they raise questions themselves - especially the second one.

1. Declare columnsToDuplicate just as it was in post #1 but then force it to an array with CVar
This is really like using the parentheses as in post #4 (we think)
Rich (BB code):
  Dim columnsToDuplicate As Variant
 
  columnsToDuplicate = Array(1, 3)
  table.Range.RemoveDuplicates Columns:=CVar(columnsToDuplicate), Header:=xlYes


2. Declare columnsToDuplicate as an array - but then leave out the names of the parameters in the RemoveDuplicates line. That seems really weird to me. :eek:
Rich (BB code):
  Dim columnsToDuplicate() As Variant
 
  columnsToDuplicate = Array(1, 3)
  table.Range.RemoveDuplicates columnsToDuplicate, xlYes
 
Upvote 0
#2 leave out the names of the parameters
that is bizarre

Does this work?
Rich (BB code):
  Dim columnsToDuplicate As Variant
 
  columnsToDuplicate =(Array(1, 3))
  table.Range.RemoveDuplicates Columns:=columnsToDuplicate, Header:=xlYes
 
Upvote 0
Does this work?
No, the last line of that errors:
1588920258317.png
 
Upvote 0

Forum statistics

Threads
1,214,835
Messages
6,121,880
Members
449,057
Latest member
Moo4247

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