VBA:删除集合中的重复项

我正在努力完成的

我想从select的单元格中选取一系列值,然后填写一个新的单元格,其中只包含我所做的select中唯一的值。 这是我的代码到目前为止:

Const Delimiter = ", " Dim num As Range Dim a As Variant Dim Concat, bucket As New Collection #to create a collection that contains all the values from my selection For Each num In Selection a = Split(num, Delimiter) Concat.Add (a) Next num #to convert multidimensional collection to a single dimensional For i = 1 To Concat.Count For j = 1 To Concat(i).Count bucket.add(Concat(i)(j)) Next i Next j #to remove duplicate values [code] #to output to excel [code] 

正如你所看到的,代码是不完整的。 我有以下几行代码的问题

 For j = 1 To Concat(i).Count 

我得到一个“运行时错误'424':对象需要”错误。

使用字典将使它更简单和简单。 见下文。

 Sub UniqueValues() Const Delimiter = "," Dim num As Range Dim a As Variant Dim i As Integer, j As Integer Dim dctData As New Dictionary 'Loop through all the values in the cells (including splitting into arrays) For Each num In Selection a = Split(num, Delimiter) For j = 0 To UBound(a, 1) If Not dctData.Exists(Trim(a(j))) Then dctData.Add Trim(a(j)), "" End If Next j Next num '#to output to excel For Each a In dctData Debug.Print a Next a End Sub