VBA字典的词典?

我正在使用字典对象在列标题下我的工作表中的每一列创build独特的集合。 列标题在第1行。

问题是我的字典包含以前的专栏的独特的项目。 例如,如果调用第4列的字典,则其包含来自第1,2,3列的所有唯一项目和标题。 我只需要来自该专栏的独特的项目。 任何想法如何纠正这个代码?

Sub cre_Dict() Dim fulArr As Variant Set d = CreateObject("scripting.dictionary") With Sheets("Database") fulArr = .Range("A1:IO27") 'assign whole table to array For j = 1 To UBound(fulArr, 2) 'looping from 1st column to last column For i = 2 To UBound(fulArr, 1) 'looping from row2 to last row If Len(fulArr(i, j)) > 0 Then 'if not blank cell d00 = d.Item(fulArr(i, j)) 'add to dictionary End If Next i d(fulArr(1, j)) = d.keys 'create dictionary under column heading Next j End With End Sub 

谢谢

考虑下面的例子,根据你的代码,我做了一些小改动:

 Dim d As Object Sub cre_Dict() Dim fulArr As Variant Dim q As Object Dim j As Long Dim i As Long Set d = CreateObject("Scripting.Dictionary") fulArr = Sheets("Database").Range("A1:IO27") 'assign whole table to array For j = 1 To UBound(fulArr, 2) 'looping from 1st column to last column Set q = CreateObject("Scripting.Dictionary") For i = 2 To UBound(fulArr, 1) 'looping from row2 to last row If Len(fulArr(i, j)) > 0 Then 'if not blank cell q(fulArr(i, j)) = Empty 'add to dictionary End If Next i d(fulArr(1, j)) = q.Keys 'create dictionary under column heading Next j End Sub 

如果我有这个正确的,你有不同的列重复。 所以基本上你在字典中的键不是唯一的,你迭代列。 如果是这种情况,当您将密钥添加到字典中时,请创build一个组合密钥。 比如像这样的东西:

Dim sKey as string sKey =“A〜”&“ABCD”

这将使列A的唯一键不变。处理集合时,可以去除每列的“A〜”部分。