0

I have the following code to remove duplicates in Excel:

    AlertRange.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, _
    7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, _
    34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49), Header:=xlNo

It works fine. But could there be a better way to provide the array to the "Columns" parameter? The way it codes looks stupid.

2
  • Array(1:49)? or Array("1:49") Unsure if that works the same as Range() or Columns() Commented Jan 28, 2015 at 14:48
  • @Chrismas007 - tried both but seems the syntax not working Commented Jan 28, 2015 at 15:39

1 Answer 1

3

Since RemoveDuplicates wants a Variant, zero-based, array rather than a Range you could make it with a loop:

Sub Macro()
    Dim ary(0 To 48)

    For i = 0 To 48
        ary(i) = i + 1
    Next i

    Range("$A$1:$BB$20").Select
    ActiveSheet.Range("$A$1:$BB$20").RemoveDuplicates Columns:=(ary), Header _
        :=xlNo
End Sub

Please note the (ary)
This is to accommodate a very old VBA bug.

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

2 Comments

+1 good point with the (ary). For more info this might be usefull link.
@Gary'sStudent... thx! i make it become a small routine to generate the array.

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.