7

When the Columns parameter of the RemoveDuplicates is passed using a variable it fails and throws error. Same code works when the columns are passed directly as Array(1,2)

Error 5: Invalid procedure call or argument

 Sub test()

       Dim arrCols

       arrCols = Array(1, 2)

       '/This here works       
       Sheet1.Range("$A$1:$B$10").RemoveDuplicates Columns:=Array(1, 2), Header _
            :=xlYes

       '/ Same code fails when the columns array is passed via variable
       '/ Error 5: Invalid procedure call or argument
        Sheet1.Range("$A$1:$B$10").RemoveDuplicates Columns:=arrCols, Header _
            :=xlYes

 End Sub

3 Answers 3

8

Put () around the array:

Sub test()

       Dim arrCols As Variant

       arrCols = Array(1, 2)

       '/This here works
       Sheet1.Range("$A$1:$B$10").RemoveDuplicates Columns:=Array(1, 2), Header _
            :=xlYes

       '/ Same code fails when the columns array is passed via variable
       '/ Error 5: Invalid procedure call or argument
        Sheet1.Range("$A$1:$B$10").RemoveDuplicates Columns:=(arrCols), Header _
            :=xlYes

 End Sub

It has to do with what vba is expecting to see.

Sign up to request clarification or add additional context in comments.

4 Comments

awesome. answer will be accepted as soon as the time permits. :)
This is a workaround rather than a fix, because there actually appears to be a problem with the RemoveDuplicates method rather than with VBA. It expects an array of Variants, and it (incorrectly) rejects a single variant containing an array, which arrCols is, so another workaround would be to declare Dim arrCols() As Variant - but for that to work, one would also have to not use the named parameter for Columns: .RemoveDuplicates arrCols, Header:=xlYes works given Dim arrCols() As Variant. VBA is happy to pass an array either way, the error occurs at runtime inside the method.
This post is over two years old. But sounds like you should put all that in another answer so others who may encounter this problem can see the two different methods. @GSerg
In Excel 2016, the array doesn't need () and works fine, but it gets error when running the code on office 365. Thanks for solving my problem!
2

Here is my take on this:

  1. I use the Evaluate function to create an array of number series - comes in handy if you need a long series as you can just change the column letters to get a different series

  2. ReDim the array so that its base becomes zero - otherwise RemoveDuplicate will throw error for some reason

  3. Use said workaround putting parenthesis around the array when calling RemoveDuplicate

_

Dim arrayTEMP as Variant
arrayTEMP = Application.Evaluate("column(A:Z)")
ReDim Preserve arrayTEMP(0 To UBound(a) - 1) As Variant
.RemoveDuplicates Columns:=(arrayTEMP), Header:=xlYes

1 Comment

For some reason, out of all the answers here, this one actually did the trick. Changing the array base to 0 instead of 1 is what made it work for me.
0

Check, if you have option base 1 in top of module. It might affect the code execution apparently.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.