1
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?

【VBA】Dictionaryオブジェクト(連想配列)

1
Posted at

やること

 Dictionaryオブジェクト(連想配列)についてのメモ。

最初に

Dictionary オブジェクト | Microsoft Learn

 事例として以下のサイトも紹介。

VBAのDictionaryの使い方(全メソッドとプロパティ網羅) | Excel作業をVBAで効率化

Dictionaryはとても高速に動作するため大量のデータを使う場合には有用な手段の1つになります。

 実は以下のサイトを見て頂ければ、今回のネタは終了とも言えます:sweat_smile:

Dictionary(ディクショナリー)連想配列の使い方について|VBA技術解説

「Dictionaryオブジェクトについて簡単な使用例を上げて解説して欲しいです。」
との要望をいただいたので、Dictionaryについて基本的な使い方を解説します。

事前確認:Qiitaでの記事

 ということで、以下の検索をしたところ、277件ヒットした。(2026/07/11時点)
「Scripting.Dictionary」の検索結果 - Qiita

 この中から、適当に以下紹介。

注意点

AccessのVBAについて、ループ内でAs New使う時は注意ね!(別インスタンスにならないから) #Windows10 - Qiita
最終更新日 2020年12月28日 投稿日 2020年11月21日

【そんなもん使うな】Scripting.Dictionaryはくそ遅いという話 #.NET - Qiita
投稿日 2019年09月12日

VBAで Dictionary にハマった話 #Excel - Qiita
投稿日 2026年03月21日

VBScriptのディクショナリの参照の罠 #VBScript - Qiita
最終更新日 2016年05月20日 投稿日 2016年05月20日

【VBA】Dictionaryでカウント処理を書くなら知っておきたい1行テクニック #dictionary - Qiita
投稿日 2025年12月16日

【ExcelVBA】DictionaryのItemにユーザー定義型を指定できない #Excel - Qiita
最終更新日 2019年06月30日 投稿日 2019年04月29日

使用例

【VBA Dictionary オブジェクト】 使い方 まとめ #初心者 - Qiita
最終更新日 2023年09月14日 投稿日 2023年09月14日

【VBScript】連想配列について #vbs - Qiita
投稿日 2023年03月20日

VBA / Dictionaryの(Keyではなく)Item値でソートする #sort - Qiita
投稿日 2022年02月12日

VBA-DictionaryでWin/mac共通のVBAコードを書く #Excel - Qiita
最終更新日 2020年09月21日 投稿日 2020年09月21日

参照設定の一覧を作るExcelマクロ #VBA - Qiita
投稿日 2020年01月30日

Excelのテーブルをオブジェクトの集合へ変換する関数 #VBA - Qiita
投稿日 2017年11月25日

【Excel VBA】ブックの名前一覧の辞書化+Exists【関数サンプル】 #VBA - Qiita
最終更新日 2021年10月05日 投稿日 2019年08月01日

VBAでのDictionaryを使ってデータを振り分け方法 #Excel - Qiita
最終更新日 2019年06月05日 投稿日 2019年06月04日

[VBA] Dictionaryの使い方。簡単なpivotを作る。 #VBA - Qiita
最終更新日 2021年05月25日 投稿日 2021年04月01日

休日祝日判定プログラム #VBA - Qiita
最終更新日 2024年06月04日 投稿日 2024年06月04日

AグループとBグループにあるものとないものを可視化 #Excel - Qiita
投稿日 2022年02月22日

Excel Dictionary 入れ子の例 #VBA - Qiita
最終更新日 2026年04月05日 投稿日 2022年04月30日

Excel VBA 住所データ処理を約2倍高速化した都道府県補完ツールの改善ポイント #ExcelVBA - Qiita
投稿日 2026年06月01日

VBAでDictionary(連想配列)を辞書順にソートする #VBA - Qiita
最終更新日 2019年08月08日 投稿日 2015年03月08日

【Excel VBA】ピボットテーブルは使わない。Dictionary(連想配列)で高速にクロス集計するテクニック #VBA - Qiita
投稿日 2026年01月23日

【5分でVBA学習】マクロ処理高速化② Dictionary(連想配列)オブジェクトの使い方 #初心者 - Qiita
最終更新日 2025年12月02日 投稿日 2025年12月02日

VBAのDictionaryオブジェクトの使い方 #Excel - Qiita
投稿日 2021年10月15日

配列にディクショナリを設定できた(VBScript) #VBScript - Qiita
最終更新日 2024年03月09日 投稿日 2024年03月09日

scripting.dictionaryについて #備忘録 - Qiita
投稿日 2025年05月13日

Excel VBAでディクショナリ(連想配列)を使用したい #ExcelVBA - Qiita
投稿日 2025年02月21日

Collectionとdictionary

VBAでCollectionとdictionaryを使う #VBA - Qiita
最終更新日 2019年07月08日 投稿日 2016年07月15日

VBAで使えるデータ構造とメリット・デメリット #VBA - Qiita
最終更新日 2020年11月05日 投稿日 2018年03月09日

【Excel VBA】配列・Dictionary・Collection・Typeの違いと使い分け【備忘録】 #Excel - Qiita
投稿日 2025年07月17日

Dictionaryを強化 / 応用

VBA VBS Scripting.Dictionary Class #VBScript - Qiita
最終更新日 2019年02月23日 投稿日 2019年02月13日

【VBA Tips】Dictionaryで配列の特定範囲だけを加算する処理を実装してみた #dictionary - Qiita
最終更新日 2025年12月16日 投稿日 2025年12月16日

【VBA】Dictionary+配列の更新処理を関数化してみた #dictionary - Qiita
最終更新日 2025年12月16日 投稿日 2025年12月16日

VBScriptで配列の重複を削除する関数を作成してみた #VBScript - Qiita
最終更新日 2021年05月04日 投稿日 2021年05月04日

VBAで重複のない配列(ユニーク,一意の配列)を作成する関数 #Excel - Qiita
投稿日 2019年09月04日

エクセルVBAで重複を除いた数を数える #ExcelVBA - Qiita
最終更新日 2019年12月25日 投稿日 2019年12月25日

Excel VBAで与えられた配列から重複しない一意な要素とそれぞれの出現回数を抽出 #Excel - Qiita
最終更新日 2024年09月13日 投稿日 2023年12月29日

VBAで重複チェック #VBA - Qiita
最終更新日 2018年12月25日 投稿日 2018年11月19日

事例

 以下、EXCEL、Word、PowerPointなどで動きます。

dictionary_test.bas
Option Explicit
'-------------------------------------------------------------------
' 関数: dictionary_test
' 説明:
'   Scripting.Dictionary オブジェクトの基本操作
'    (生成・追加・参照)を確認するテスト用関数
'
'   <生成>
'   ・Dictionary の生成方法 2 パターン
'     ※ 使用前に「参照設定」で「Microsoft Scripting Runtime」に
'        チェックを入れること。
'   ・参照設定なし(遅延バインディング)の方法 1 パターン
'   <追加>
'   ・Key と Item の追加方法 2 パターン
'   <参照>
'   ・特定キーの値取得
'   ・Keys / Items を使った全要素の列挙
'
' 注意:
'   ・使用前に「参照設定」で「Microsoft Scripting Runtime」に
'     チェックを入れること。
'   ・Dictionary は Key の重複を許可しない。
'   ・Keys(i), Items(i) は 0 から Count-1 のインデックスでアクセス可能。
'
' 使用例:
'   Call dictionary_test
'
' 戻り値:
'   なし(MsgBox に結果を表示)
'-------------------------------------------------------------------
Sub dictionary_test()
'   <生成>
    '---------------------------------------------
    ' 変数設定
    '---------------------------------------------
    Dim i As Integer    ' ループ用
    Dim s As String     ' MsgBox 表示用文字列

    ' Dictionary の生成(パターン1)
'     Dim tests As New Scripting.Dictionary
    
    ' Dictionary の生成(パターン2)
'     Dim tests As Scripting.Dictionary
'     Set tests = New Scripting.Dictionary

    ' 参照設定なし(遅延バインディング)の場合
    Dim tests As Object
    Set tests = CreateObject("Scripting.Dictionary")
    Dim keysArr As Variant
    Dim itemsArr As Variant

'   <追加>
    '---------------------------------------------
    ' Key と Item の追加
    '---------------------------------------------
    ' Key と Item の追加(パターン1)
'     tests.Add Key:="A", Item:="あいうえお"
'     tests.Add Key:="B", Item:="いろはにほへと"
'     tests.Add Key:="C", Item:="AABBCCDDEE"
'     tests.Add Key:="D", Item:="永氷水"

    ' Key と Item の追加(パターン2)
     tests.Add "A", "あいうえお"
     tests.Add "B", "いろはにほへと"
     tests.Add "C", "AABBCCDDEE"
     tests.Add "D", "永氷水"

'   <参照>
    '---------------------------------------------
    ' 特定キーの値を取得して表示
    '---------------------------------------------
    MsgBox tests.Item("B")

    '---------------------------------------------
    ' Dictionary の全要素を列挙して文字列化 / 表示
    '---------------------------------------------
    ' 参照設定なし(遅延バインディング)の場合 ここから
    keysArr = tests.Keys
    itemsArr = tests.Items
    ' 参照設定なし(遅延バインディング)の場合 ここまで
    
    s = "dictionary" & vbCrLf

    For i = 0 To tests.Count - 1
    ' 参照設定あり(事前バインディング)の場合
'        s = s & tests.Keys(i) & "=" & tests.Items(i) & vbCrLf
    ' 参照設定なし(遅延バインディング)の場合
        s = s & keysArr(i) & "=" & itemsArr(i) & vbCrLf
    Next

    ' 結果を表示
    MsgBox s

End Sub

 注意にも書きましたが、事前(早期)バインディングの「Dictionary の生成(パターン1)」及び「(パターン2)」では、使用前に「参照設定」で「Microsoft Scripting Runtime」にチェックを入れることをお忘れなく。
image.pngimage.png
 Wordでも動くはずです。
(「Scripting.Dictionary」を「Dictionary」にすると、Wordでは動かないので、昔ハマった:sweat_smile:)
スクリーンショット 2026-07-11 175937.pngスクリーンショット 2026-07-11 180441.pngスクリーンショット 2026-07-11 181646.png

1
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
1
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?