You could fix the code like that
(您可以像这样修复代码)
Sub unique()
Dim arr As New Collection, a
Dim aFirstArray As Variant
Dim i As Long
aFirstArray = Worksheets("Sheet1").Range("A2", Range("A2").End(xlDown))
On Error Resume Next
For Each a In aFirstArray
arr.Add a, CStr(a)
Next
On Error GoTo 0
For i = 1 To arr.Count
Cells(i, 2) = arr(i)
Next
End Sub
The reason for your code failing is that a key must be a unique string expression, see MSDN
(代码失败的原因是键必须是唯一的字符串表达式,请参见MSDN。)
Update : This is how you could do it with a dictionary.
(更新 :这是您可以使用字典的方式。)
You need to add the reference to the Microsoft Scripting Runtime (Tools/References): (您需要将引用添加到Microsoft脚本运行时(工具/参考):)
Sub uniqueA()
Dim arr As New Dictionary, a
Dim aFirstArray As Variant
Dim i As Long
aFirstArray = Worksheets("Sheet1").Range("A2", Range("A2").End(xlDown))
For Each a In aFirstArray
arr(a) = a
Next
Range("B1").Resize(arr.Count) = WorksheetFunction.Transpose(arr.Keys)
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…