【发布时间】:2018-10-08 12:10:30
【问题描述】:
我想将项目列表添加到集合中并避免添加重复项。 这是我在 A 列中的列表
Apple
Orange
Pear
Orange
Orange
Apple
Carrot
我只想添加
Apple
Orange
Pear
Carrot
这是我想出的,它有效,但并不漂亮。
dim coll as New Collection
ln = Cells(Rows.Count, 1).End(xlUp).Row
coll.Add (Cells(1, 1).Value) 'Add first item manually to get it started
For i = 1 To ln
addItem = True 'Assume it's going to be added until proven otherwise
For j = 1 To coll.Count 'Loop through the collection
'If we ever find the item in the collection
If InStr(1, Cells(i, 1), coll(j), vbTextCompare) > 0 Then
addItem = False 'set this bool false
End If
Next j
If addItem = True Then 'It never got set to false, so add it
coll.Add (Cells(i, "A").Value)
End If
Next i
有没有更简单的方法来做到这一点?最好是像
If Not coll.Contains(someValue) Then
coll.Add (someValue)
End If
【问题讨论】:
-
使用字典?来自 Microsoft Scrtping Runtime 库。