Dictionary

オブジェクト

1. はじめに

VBAで複数の値を関連付けて扱いたい場合、通常は配列やコレクションを使います。
しかし、キーと値の組み合わせ(連想配列)でデータを扱いたいときに最も適しているのが Dictionary オブジェクト です。

これは Scripting.Dictionary クラスとして提供されており、以下のような操作が簡潔に行えます:

  • キーと値のペアを追加・削除・検索
  • 値の上書き、存在確認、キー一覧の取得
  • 重複のないデータ管理や集計処理

2. Dictionaryの利用準備

2-1. 参照設定(推奨)

使用するには、以下の参照設定を行うと便利です:

[ツール] > [参照設定] > Microsoft Scripting Runtime

これにより、型宣言や補完機能が使用可能になります。

2-2. インスタンス作成

Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary

※参照設定を行わない場合は以下のように記述:

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

3. Dictionaryの基本操作

3-1. 要素の追加(Add)

dict.Add "ID001", "山田"
dict.Add "ID002", "佐藤"
  • キーは重複不可。重複するとエラーになります。
  • キーは文字列だけでなく、数値や日付などでも可能。

3-2. 要素の取得(Item または dict(key))

MsgBox dict("ID001")         ' → 山田
MsgBox dict.Item("ID002") ' → 佐藤

3-3. 要素の上書き

dict("ID001") = "田中"  ' 上書き可能

4. 要素の存在確認と削除

4-1. 存在確認(Exists)

If dict.Exists("ID003") Then
MsgBox "既に登録済み"
Else
dict.Add "ID003", "鈴木"
End If

4-2. 削除(Remove)

dict.Remove "ID002"

すべて削除する場合:

dict.RemoveAll

5. キーと値の一覧取得

5-1. キーの取得(Keys)

Dim k As Variant
For Each k In dict.Keys
Debug.Print k
Next

5-2. 値の取得(Items)

Dim v As Variant
For Each v In dict.Items
Debug.Print v
Next

6. Dictionaryの主なプロパティ・メソッド

プロパティ/メソッド内容
.Add key, itemキーと値のペアを追加
.Item(key)キーに対応する値の取得/設定
.Exists(key)キーの存在確認(Boolean)
.Remove(key)指定したキーの削除
.RemoveAll全データの削除
.Count要素数
.Keysキーの配列
.Items値の配列

7. Dictionaryの活用例

7-1. ユニークデータの抽出

Dim cell As Range
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

For Each cell In Range("A2:A100")
If Not dict.Exists(cell.Value) Then
dict.Add cell.Value, 1
End If
Next

' 結果出力
Range("C2").Resize(dict.Count).Value = Application.Transpose(dict.Keys)

7-2. 集計処理(カウント)

Dim value As Variant
For Each cell In Range("B2:B100")
value = cell.Value
If dict.Exists(value) Then
dict(value) = dict(value) + 1
Else
dict.Add value, 1
End If
Next

' 集計結果表示
Dim i As Long: i = 2
For Each k In dict.Keys
Cells(i, 5).Value = k
Cells(i, 6).Value = dict(k)
i = i + 1
Next

8. DictionaryとCollectionの違い

特徴DictionaryCollection
キーの指定明示的(文字列・数値)文字列キー使用可能だが制限あり
キーの存在確認.Exists(key) が使える存在確認不可(エラーになる)
上書きキーが同じなら上書きできる上書き不可
要素数取得.Count.Count
並び順保証されない挿入順

9. Dictionary使用時の注意点

  • キーは大小文字を区別しない(CompareMode を変更可能)
  • Variant 型を使用するため、配列やオブジェクトも格納可能
  • 多くのデータを扱う場合は .Exists を活用して高速化が可能

10. 高度なテクニック:ネストされたDictionary

Dictionaryの中にDictionaryを入れることで、多階層構造のデータも扱えます。

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

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

subDict.Add "Math", 80
subDict.Add "English", 90

mainDict.Add "山田", subDict

MsgBox mainDict("山田")("English") ' → 90

11. 実用例:VLOOKUPの代替

' A列にID、B列に名前があると仮定
Dim id As Variant, name As Variant
For i = 2 To 100
id = Cells(i, 1).Value
name = Cells(i, 2).Value
dict(id) = name
Next

' IDから名前を取得
Dim inputID As String
inputID = InputBox("IDを入力してください")

If dict.Exists(inputID) Then
MsgBox "名前:" & dict(inputID)
Else
MsgBox "該当するIDが見つかりません"
End If

12. まとめ

特徴内容
オブジェクト名Scripting.Dictionary
用途キーと値のペアでデータを格納/取得/管理
主な操作Add, Item, Exists, Remove, Keys, Items
配列との違いキーでアクセス、順番に依存しない
Collectionとの違いキーの存在確認や上書きが可能
代表的な用途ユニーク抽出、カウント集計、キー検索、データマッピングなど
タイトルとURLをコピーしました