我發現一箇舊的方法http://www.techbookreport.com/tutorials/vba_dictionary2.html在VBA中的字典中執行字典,但在Scripting庫中的Excel 2013修改中,我無法使嵌套工作的方式相同。字典中的字典
或者有嗎?字典
Sub dict()
Dim ws1 As Worksheet: Set ws1 = Sheets("BM")
Dim family_dict As New Scripting.Dictionary
Dim bm_dict As New Scripting.Dictionary
Dim family As String, bm As String
Dim i
Dim ws1_range As Range
Dim rng1 As Range
With ws1
Set ws1_range = .Range(Cells(2, 1).Address & ":" & Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1).Address)
End With
For Each rng1 In ws1_range
family = ws1.Cells(rng1.Row, 1)
bm = ws1.Cells(rng1.Row, 2)
If family_dict.Exists(family) Then
Set bm_dict = family_dict(family)("scripting.dictionary")
If bm_dict.Exists(bm) Then
Else
bm_dict.Add bm, Empty
End If
Else
family_dict.Add family, Empty
Set bm_dict = family_dict(family)("scripting.dictionary")
If bm_dict.Exists(bm) Then
Else
bm_dict.Add bm, Empty
End If
End If
For Each i In family_dict.Keys: Debug.Print i: Next
For Each i In bm_dict.Keys: Debug.Print i: Next
For Each i In bm_dict.Items: Debug.Print i: Next
Debug.Print bm_dict.Count
Next
End Sub
VBA和VBScript都老了,穩定TECHNOLOGI ES。如果在2013年的字典語義發生任何變化,我會感到驚訝。 –
我見過一些這樣的詞典的字典例程,我通常爲什麼單個字典加入值作爲Item(例如像Atom提要)不能使用。\ – Jeeped
您不能使用此語法:'Set bm_dict = family_dict(family)(「scripting.dictionary」)''。您必須使用與鏈接相同的方法:「Set myDictionary = New Dictionary」,然後將其添加爲父字典的項目。我在[this](http://stackoverflow.com/a/32362499/4914662)答案中做了一個工作示例,您可以根據您的數據對其進行調整 –