考虑一个将字符串聚合为逗号分隔值字符串的循环:
Dim Result As String
For Each Something In Things
If Result <> vbNullString Then
Result = Result & ","
End If
Result = Result & SomeStringFunction(Something)
Next Something
这有效,但如果我只想要不同的字符串怎么办?我一直在使用这种方法,但它似乎非常“重量级”:
Dim Dict As Dictionary
Set Dict = New Dictionary
For Each Something In Things
Dict(SomeStringFunction(Something)) = vbNullString
Next Something
Dim Result As String
Dim vKey As Variant
For Each vKey In Dict.Keys
If Result <> vbNullString Then
Result = Result & ","
End If
Result = Result & CStr(vKey)
Next vKey
Set Dict = Nothing
答案 0 :(得分:1)
好的,糟糕的黑客时间:
Dim Result As String
Dim noDupes as New Collection
For Each Something in Things
On Error Resume Next
noDupes.Add Something, Something
If Err.Number = 0 Then
If Result <> vbNullString Then
Result = Result & ","
End If
Result = Result & SomeStringFunction(Something)
End If
Err.Clear
Next Something