hakeの日記

Windows環境でプログラミングの勉強をしています。

EXCEL VBAメモ - 連想配列 Dictionary

    Dim dic As Object
    Dim d As Variant   ' Objectではダメ
    
    Set dic = CreateObject("Scripting.Dictionary")
    dic("a") = "A"       ' データ追加
    dic.Add "b", "B"     ' データ追加、キーが存在した場合はエラー
    If Not dic.Exists("c") Then ' キーの存在確認
        dic.Add "c", "C"
    End If
    MsgBox dic.Count     ' データ数
    MsgBox dic.Item("a") ' キーに対応するItem表示
    
    
    For Each d In dic.Keys ' 全キー表示
        MsgBox d
    Next
    For Each d In dic.Items ' 全Item表示
        MsgBox d
    Next
    dic.Remove ("a") ' データ削除
    For Each d In dic.Items ' 確認
        MsgBox d
    Next
    dic.RemoveAll ' 全データ削除

    Set dic = Nothing