【发布时间】:2016-08-14 19:09:46
【问题描述】:
首先,我想说,我已经通过网络搜索过,但我还没有遇到过这样的事情。我见过集合的集合,或数组的数组,但不是数组的集合。
我想要做的是,收集每个区的集合中的 ID。最后,我将使用 Join 函数和“;”加入集合中的值作为分隔符,然后在每个类的 4 列范围内打印它们作为查找列表。例如;
Class2(0) 将包括 54020 和 30734,class2(1) 将包括 58618,class1(4) 将包括 none,class3(7) 将包括 35516、34781 和 56874,依此类推。
我想遍历 C 列并放置一个 select case 语句来检查类,然后将值分配给集合
Sub dict_coll()
Dim class1() As New Collection
Dim class2() As New Collection
Dim class3() As New Collection
Dim class4() As New Collection
Dim dict As New Scripting.Dictionary
Set dRange = range(range("a2"), range("a2").End(xlDown))
i = 0
For Each d In dRange
If Not dict.Exists(d.Value) Then
dict.Add key:=d.Value, item:=i
i = i + 1
End If
Next d
Set cRange = range(range("c2"), range("c2").End(xlDown))
For Each c In cRange
Select Case c.Value
Case "class1"
class1(dict(c.offset(0,-2).value)).Add c.Offset(0, 1).Value 'fails here
Case "class2"
class2(dict(c.offset(0,-2).value)).Add c.Offset(0, 1).Value 'fails here
Case "class3"
class3(dict(c.offset(0,-2).value)).Add c.Offset(0, 1).Value 'fails here
Case Else
class4(dict(c.offset(0,-2).value)).Add c.Offset(0, 1).Value 'fails here
End Select
Next c
End Sub
我想看到的是如下: 我想做的事情有什么更简单和正确的方法吗?任何帮助将不胜感激。
谢谢
【问题讨论】:
-
在第3段中,我错误地写了class1(7010)等,但实际上它应该是class1(0),因为字典。对此感到抱歉。
-
VBA Dictionary of dictionaries 你在找什么吗?
-
我不确定,也许是,但我不知道该怎么做
-
目前还不清楚您想要获得的数据结构是什么。请编辑您的问题以详细说明您想要什么结构并更正第 3 段。
-
嗨欧米茄,我插入了我想要的输出
标签: arrays excel vba dictionary collections