代码:
Dim PrefixDict, k, j
Dim renamePrefix As String
Set PrefixDict = CreateObject("Scripting.Dictionary")
For k = 1 To fileList.Count
renamePrefix = renamePrefix + fileList.Item(k).renamePrefix + IIf(k < fileList.Count, ", ", " - ")
PrefixDict(fileList(k).FilePattern) = renamePrefix
Next k
For Each j In PrefixDict
Debug.Print j, PrefixDict(j)
Next
输出(立即窗口):
Sat_Net_Out_PORTME*.xlsx r10, r20, r30,
PRINTIMAGE*.txt r10, r20, r30, r40,
Sat_Net_Out_OMAHA*.xlsx r10, r20, r30, r40, r50 -
预期输出(立即窗口):
Sat_Net_Out_PORTME*.xlsx r10, r20, r30 -
PRINTIMAGE*.txt r40 -
Sat_Net_Out_OMAHA*.xlsx r50 -
我正在尝试为fileList(对象集合)中的每个.FilePattern创建一个字符串,其中包含与每个项目相匹配的每个项目的.renamePrefix的连接.FilePattern
我认为我错误地设置了For循环,因为它遍历集合中的每个项目,而不是集合中的每个.FilePattern。基本上,我想为每个.FilePattern单独的renamePrefix字符串。无论如何都要正确设置吗?
编辑:
收藏品
.FilePattern .renamePrefix
Sat_Net_Out_PORTME*.xlsx r10
Sat_Net_Out_PORTME*.xlsx r20
Sat_Net_Out_PORTME*.xlsx r30
PRINTIMAGE*.txt r40
Sat_Net_Out_OMAHA*.xlsx r50
答案 0 :(得分:3)
在第一个循环中,您无法知道何时为任何特定键添加了最后一项,因此您必须稍后添加短划线
Dim PrefixDict, k, j, p
Dim renamePrefix As String
Set PrefixDict = CreateObject("Scripting.Dictionary")
For k = 1 To fileList.Count
p = fileList(k).FilePattern
renamePrefix = fileList.Item(k).renamePrefix '<<<EDIT
If Not PrefixDict.exists(p) Then
PrefixDict(p) = renamePrefix
Else
PrefixDict(p) = PrefixDict(p) & ", " & renamePrefix
End If
Next k
For Each j In PrefixDict
PrefixDict(j) = PrefixDict(j) & " - "
Debug.Print j, PrefixDict(j)
Next