7
0

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のDictionaryオブジェクトの使い方

Posted at

はじめに

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

終わりに

簡単ではありますが、調べることは結構あったりするので簡単な使用方法をまとめてます。
今後はこの記事を参考に書いていければよいなと思います。

7
0
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
7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?