2
4

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とCollectionでよく使うものまとめてみた

Posted at

vbaで連想配列を実現するためには、dictionaryとcollectionの2種類存在する。ここではdictionaryとcollectionのそれぞれについて値の追加・取り出し方を記載していく。collection/dictionaryともに基本的な機能は同じだが使用できるプロパティ・メソッドに違いがるので、それぞれが有している機能については下記の公式リファレンスを参照してもらうとよい。

Dictionary Object

A Dictionary object is the equivalent of a PERL associative array. Items, which can be any form of data, are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.Office デベロッパーセンター「Dictionary object」https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/dictionary-object

dictionaryの宣言と初期化

Dim testDic As Object
Set testDic = CreateObject("Scripting.Dictionary");

dictionaryに要素を追加

// Dictionary.Add Key, Value

With testDic
    .Add "A", "神奈川"
    .Add "B", "岩手"
    .Add "C", "静岡"
End With

dictionaryの値を取り出す

// Keyを指定して値を取り出す

testDic("A") // 神奈川
testDic.Item("B") // 岩手
// For Eachを使用して値を取り出す

Dim k As Variant
For Each k In testDic.Keys
  Debug.Print "Key: " & k & ", Value: " & testDic(k)
  // 出力:
  //  Key: A, Value: 神奈川
  //  Key: B, Value: 岩手
  //  Key: C, Value: 静岡
Next key

Dim i As Variant
For Each i in testDic.Items
  Debug.Print i
  // 出力:
  //  神奈川
  //  岩手
  //  静岡
Next item

dictionaryにdictionaryを追加する

連想配列の値に連想配列を指定することもvbaでは可能

Dim childDic As Object
Set childDic = CreateObject("Scripting.Dictionary")

childDic.Add "D", "福島"
childDic.Add "E", "東京"
childDic.Add "F", "鹿児島"

// testDicの値に新しくdictionaryを追加
testDic.Add "AA", childDic

For Each i In testDic.Keys
    If VarType(testDic(i)) = 9 Then
        For Each k In testDic(i).Items
            Debug.Print k
            // 出力:
            //  福島
            //  東京
            //  鹿児島
        Next
    End If
Next

dictionaryのkeyが存在するか確認する

Debug.Print testDic.Exists("A") // True

See Also

Collection Object

The Collection object provides a convenient way to refer to a related group of items as a single object. The items, or members, in a collection need only be related by the fact that they exist in the collection. Members of a collection don't have to share the same data type.Office デベロッパーセンター「Collecion object」https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/collection-object

collectionの宣言と初期化

Dim testCol As Collection
Set testCol = New Collection

collectionに要素を追加

// Collection.Add Value, Key

With testCol
    .Add "神奈川", "A"
    .Add "静岡", "B"
    .Add "岩手", "C"
End With

collectionの値を取り出す

Debug.Print testCol.Item("A") // 神奈川
Debug.Print testCol("B") // 静岡

collectionの値をループで取り出す

For Each i In testCol
  Debug.Print i
  //出力:
  // 神奈川
  // 静岡
  // 岩手
Next
2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?