はじめに
VBAの辞書を使うときに毎回調べているなと感じることが多いので記事にまとめておこうかなと思いました。
Dictionaryオブジェクトの使い方
コード
Option Explicit
Sub test()
' 生成
Dim test_dict As Object
Set test_dict = CreateObject("Scripting.Dictionary")
' 追加
test_dict.Add "test", "test"
test_dict.Add "test2", "test2"
test_dict.Add "test3", "test3"
' 参照
Debug.Print "###参照###"
Debug.Print test_dict("test")
' 更新
Debug.Print "###更新###"
test_dict("test") = "test_update"
Debug.Print test_dict("test")
' For Eachでの使い方
Debug.Print "###ループ###"
Dim var As Variant
For Each var In test_dict
Debug.Print "キーの参照:" & var
Debug.Print "値の参照:" & test_dict.Item(var)
Next var
End Sub
イミディエイトウィンドウの表示
### 参照###
test
### 更新###
test_update
### ループ###
キーの参照:test
値の参照:test_update
キーの参照:test2
値の参照:test2
キーの参照:test3
値の参照:test3
終わりに
簡単ではありますが、調べることは結構あったりするので簡単な使用方法をまとめてます。
今後はこの記事を参考に書いていければよいなと思います。