Excel VBA 逆引き集 | 巨大Dictionary

Excel VBA
スポンサーリンク

巨大Dictionaryの扱い方

Excel VBAで大量データを扱うとき、Dictionary は「キーと値のペア」を高速に管理できる便利な入れ物です。数千〜数万件のデータを扱う場合でも、Dictionaryを使えば検索や集計が一瞬でできます。ここでは初心者向けに「巨大Dictionary」を安全に効率よく扱う方法を、コード例やテンプレートを交えて説明します。


基本の考え方

  • 大量データでも高速 → Dictionaryはハッシュ構造なので検索が速い。
  • キーはユニーク → 重複を自動的に排除できる。
  • Valueに数値・文字列・配列・オブジェクトも格納可能
  • メモリ効率に注意 → 不要になったら Set dict = Nothing で解放。

テンプレ1:巨大Dictionaryにデータを格納

Sub HugeDict_Add()
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim i As Long

    ' 1万件のデータを追加
    For i = 1 To 10000
        dict("Key" & i) = "Value" & i
    Next i

    MsgBox "登録件数=" & dict.Count
End Sub
VB
  • ポイント:
    • 1万件でも一瞬で追加可能。
    • Count で件数確認。

テンプレ2:巨大Dictionaryから検索

Sub HugeDict_Search()
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim i As Long

    ' データ登録
    For i = 1 To 50000
        dict("ID" & i) = "商品" & i
    Next i

    ' 検索
    Dim key As String: key = "ID30000"
    If dict.Exists(key) Then
        MsgBox key & " → " & dict(key)
    Else
        MsgBox "該当なし"
    End If
End Sub
VB
  • ポイント: 数万件でも検索は一瞬。

テンプレ3:巨大Dictionaryで集計処理(商品別数量合計)

Sub HugeDict_GroupBy()
    Dim ws As Worksheet: Set ws = Worksheets("Data")
    Dim rg As Range: Set rg = ws.Range("A2:C10000") ' A=商品名, C=数量
    Dim v As Variant: v = rg.Value

    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim r As Long, key As String

    For r = 1 To UBound(v, 1)
        key = v(r, 1) ' 商品名
        If dict.Exists(key) Then
            dict(key) = dict(key) + v(r, 3)
        Else
            dict(key) = v(r, 3)
        End If
    Next r

    ' 出力
    Dim k As Variant, i As Long: i = 2
    For Each k In dict.Keys
        ws.Cells(i, 5).Value = k
        ws.Cells(i, 6).Value = dict(k)
        i = i + 1
    Next k
End Sub
VB
  • ポイント:
    • 商品ごとに数量を集計。
    • 数万件でも高速。

テンプレ4:巨大Dictionaryのメモリ効率化

Sub HugeDict_Memory()
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim i As Long

    ' データ追加
    For i = 1 To 20000
        dict(CStr(i)) = i
    Next i

    ' 使用後は解放
    Set dict = Nothing
End Sub
VB
  • ポイント:
    • CStr(i) でキーを文字列化 → メモリ効率が良い。
    • 不要になったら Nothing で解放。

テンプレ5:巨大Dictionaryを配列に変換して処理

Sub HugeDict_ToArray()
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim i As Long

    For i = 1 To 10000
        dict("Key" & i) = "Value" & i
    Next i

    Dim keysArr As Variant: keysArr = dict.Keys
    Dim itemsArr As Variant: itemsArr = dict.Items

    For i = LBound(keysArr) To UBound(keysArr)
        Debug.Print keysArr(i), itemsArr(i)
    Next i
End Sub
VB
  • ポイント:
    • KeysとItemsを配列化すれば一括処理が可能。

例題で練習

'例1:巨大Dictionaryにデータを格納
Sub Example1()
    HugeDict_Add
End Sub

'例2:巨大Dictionaryから検索
Sub Example2()
    HugeDict_Search
End Sub

'例3:商品別数量合計
Sub Example3()
    HugeDict_GroupBy
End Sub

'例4:メモリ効率化
Sub Example4()
    HugeDict_Memory
End Sub

'例5:配列に変換して処理
Sub Example5()
    HugeDict_ToArray
End Sub
VB

初心者向けポイント

  • 巨大Dictionaryでも高速 → 数万件の検索・集計が一瞬。
  • Existsで安全に検索 → 存在確認を必ず行う。
  • Keys/Itemsを配列化 → 一括処理に便利。
  • メモリ解放を忘れないSet dict = Nothing
  • 実務で便利 → 大量データのユニーク化、集計、検索に最適。

👉 この「巨大Dictionaryテンプレ」を覚えておけば、Excel VBAで数万件規模のデータを 高速・安全・効率的 に処理できるようになります。

タイトルとURLをコピーしました