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.

chapter3
  • 914
  • 2
  • 12
  • 21

1 Answers1

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.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
  • 2
    +1 good point with the (ary). For more info this might be usefull [link](http://stackoverflow.com/questions/5413765/what-are-the-rules-governing-usage-of-brackets-in-vba-function-calls). – Daniel Dušek Jan 28 '15 at 16:52
  • @Gary'sStudent... thx! i make it become a small routine to generate the array. – chapter3 Jan 30 '15 at 10:31