There are 2 set of codes below, the upper one (as comments) doesn't work, while the one below works. My goal is to remove duplicates from a range that I do not know its number of columns beforehand. The one above should also create an array similar to Array(1,2,3,4,5). Any idea why the one at the bottom works while the one on top doesn't? Thanks in advance! (Pasted the wrong code block earlier.)
The error I got is Run Time error 5: Invalid procedure call or argument.
Sub RemoveDup(datarange As Range)
Dim ColArray() As Variant
Dim i As Integer
' ReDim ColArray(1 To datarange.Columns.Count())
'
' For i = 1 To datarange.Columns.Count()
' ColArray(i) = i
' Next i
ColArray = Array(1, 2, 3, 4, 5)
datarange.RemoveDuplicates Columns:=(ColArray), _
Header:=xlNo
End Sub