是否可以在字典键中找到所有重复项,从键中获取值,然后将所有值组合起来保存一个键?
例如。查看下面的数据表。如果我想将其保存为字典,则将“ A”列作为键,将“ B”列作为值:
是否可以删除重复项,使我们只有一个“ A”键,其值为= Thank; You; For; Helping”?
答案 0 :(得分:2)
您可以将Collection(或Dictionary)对象或数组作为项存储在字典中。
类似的东西:
编辑:打印出字典数据
Option Explicit
Sub marine()
Dim D As Dictionary, C As Collection
Dim V, W, I As Long, sKey As String
V = Range("A1").CurrentRegion
Set D = New Dictionary
D.CompareMode = TextCompare
For I = 2 To UBound(V)
sKey = V(I, 1)
If Not D.Exists(sKey) Then
Set C = New Collection
C.Add V(I, 2)
D.Add Key:=sKey, Item:=C
Else
D(sKey).Add V(I, 2)
End If
Next I
For Each V In D.Keys
For Each W In D(V)
Debug.Print V, W
Next W
Next V
End Sub
调试打印输出
A Thank
A You
A For
A Helping
B Car
C Bus
D Late
E Damage
将为您提供每个键附带的所有项目的集合。
顺便说一句,要连接与唯一键关联的所有值,在Excel Office365中,您可以使用公式进行操作
提供您的数据:
E2: =UNIQUE($A$2:$A$9)
F2: =TEXTJOIN(";",TRUE,FILTER($B$2:$B$9,($A$2:$A$9=E2)))
选择F2
并根据需要填写
答案 1 :(得分:0)