0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VBA_02_Dictionary_入れ子_二重_Sample

Last updated at Posted at 2022-05-21

keyword

ディクショナリ
入れ子
二重
dictionaryにdictionaryを追加

Sample Code

Sample Code
Public Sub MySample20220520()

    Dim arrList As Variant
    Dim i As Long: Dim j As Long
    Dim myNameList As Dictionary: Dim myTypeList As Dictionary
    Dim myName As Variant: Dim myType As Variant

    With ThisWorkbook.ActiveSheet
        arrList = .Range("A1").CurrentRegion
    End With
    
    Set myNameList = New Dictionary
    
    For i = LBound(arrList, 1) + 1 To UBound(arrList, 1)
    
        If Not myNameList.Exists(arrList(i, 1)) Then
            
            Set myTypeList = New Dictionary
            
            myNameList.Add arrList(i, 1), myTypeList
            
            Set myTypeList = myNameList.Item(arrList(i, 1))
            
            myTypeList.Add arrList(i, 2), arrList(i, 3)
        
        Else
            
            If myTypeList Is Nothing Then
                Set myTypeList = New Dictionary
            End If
            
            Set myTypeList = myNameList.Item(arrList(i, 1))
            
            If myTypeList.Exists(arrList(i, 2)) Then
                myTypeList.Item(arrList(i, 2)) = myTypeList.Item(arrList(i, 2)) + arrList(i, 3)
            Else
                myTypeList.Add arrList(i, 2), arrList(i, 3)
            End If
        
        End If
    
    Next i
    
    Set myTypeList = New Dictionary
    
    For Each myName In myNameList.Keys
    
        Set myTypeList = myNameList.Item(myName)

        For Each myType In myTypeList.Keys
            Debug.Print myName, myType, myTypeList.Item(myType)
        Next
        
    Next

End Sub

Sample Data

無題.jpg

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?