Excel VBA distinguishes between passing an array directly (RemDuplicates1) and passing a variable containing an array (RemDuplicates3) to the method Range.RemoveDuplicates(). Why is it so and how can we assign to a variable an array, so that the variable can be accepted by the RemoveDuplicates method as a parameter.
A workaround is (RemDuplicates2) to reside the array inside a function and return it as the Column parameter of RemoveDuplicates().
Please find my tests below.
Thanks in advance for any hints and help.
Option Base 0
' Test environment:
' Microsoft® Excel® for Microsoft 365 MSO (Version 2303 Build 16.0.16227.20202) 64 Bit
' Windows 10 64 Bit
' Selection is of type Range
Sub RemDuplicates1()
' This works.
Selection.RemoveDuplicates Array(1, 2)
End Sub
Sub RemDuplicates2()
' This works.
Selection.RemoveDuplicates ReturnArray
End Sub
Function ReturnArray()
ReturnArray = Array(1, 2)
End Function
Sub RemDuplicates3()
' This does not work.
Dim Arr
Arr = Array(1, 2)
Selection.RemoveDuplicates Arr
End Sub