Working code for my worksheet:
Sub dict()
Dim ws1 As Worksheet: Set ws1 = Sheets("BM")
Dim family_dict As Dictionary, bm_dict As Dictionary
Dim i, j
Dim ws1_range As Range
Dim rng1 As Range, rng2 As Range
With ws1
Set ws1_range = .Range(Cells(2, 1).Address & ":" & Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1).Address)
End With
Set family_dict = New Dictionary
For Each rng1 In ws1_range
If Not family_dict.Exists(Key:=ws1.Cells(rng1.Row, 1).Value2) Then
Set bm_dict = New Dictionary
For Each rng2 In ws1_range
If rng2 = rng1 Then
If Not bm_dict.Exists(Key:=ws1.Cells(rng2.Row, 2).Value2) Then
bm_dict.Add Key:=ws1.Cells(rng2.Row, 2).Value2, Item:=Empty
End If
End If
Next
family_dict.Add Key:=ws1.Cells(rng1.Row, 1).Value2, Item:=bm_dict
Set bm_dict = Nothing
End If
Next
'---test---immediate window on---
For Each i In family_dict.Keys: Debug.Print i: For Each j In family_dict(i): Debug.Print j: Next: Next
End Sub
source
share