使用 Scripting.Dictionary 获取唯一值
Dictionary
允许非常简单地获得一组独特的值。考虑以下功能:
Function Unique(values As Variant) As Variant()
'Put all the values as keys into a dictionary
Dim dict As New Scripting.Dictionary
Dim val As Variant
For Each val In values
dict(val) = 1 'The value doesn't matter here
Next
Unique = dict.Keys
End Function
然后你可以像这样调用:
Dim duplicates() As Variant
duplicates = Array(1, 2, 3, 1, 2, 3)
Dim uniqueVals() As Variant
uniqueVals = Unique(duplicates)
而 uniqueVals
只包含 {1,2,3}
。
注意:此函数可与任何可枚举对象一起使用。