Excel VBA 逆引き集 | Dictionary高速テンプレ(差分)

Excel VBA
スポンサーリンク

Dictionary高速テンプレ(差分処理)

Excel VBAで「差分処理」とは、2つのリストやデータ集合を比較して違いを抽出することです。
例えば「前回の顧客リストと今回の顧客リストの差分を確認したい」「新規追加された商品だけを抽出したい」といった場面で役立ちます。
Dictionaryを使えば高速に差分を求められます。初心者向けに、コード例やテンプレートをかみ砕いて説明します。


基本の考え方

  • Dictionaryはキー重複不可 → 登録済みかどうかを高速に判定できる。
  • Existsメソッド → キーが存在するかどうかを確認。
  • 差分処理のパターン:
    • AにあってBにないもの(追加分)
    • BにあってAにないもの(削除分)
    • 両方にあるもの(共通分)

テンプレ1:2つの配列の差分(AにあってBにない)

Sub Dict_Diff_A_NotInB()
    Dim arrA As Variant, arrB As Variant
    arrA = Array("りんご", "みかん", "バナナ")
    arrB = Array("りんご", "ぶどう")

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

    ' Bの要素を登録
    For Each item In arrB
        dict(item) = True
    Next item

    ' AにあってBにないものを抽出
    For Each item In arrA
        If Not dict.Exists(item) Then
            Debug.Print "差分(Aのみ)=" & item
        End If
    Next item
End Sub
VB
  • 結果: 差分(Aのみ)=みかん 差分(Aのみ)=バナナ

テンプレ2:2つの配列の差分(BにあってAにない)

Sub Dict_Diff_B_NotInA()
    Dim arrA As Variant, arrB As Variant
    arrA = Array("りんご", "みかん", "バナナ")
    arrB = Array("りんご", "ぶどう")

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

    ' Aの要素を登録
    For Each item In arrA
        dict(item) = True
    Next item

    ' BにあってAにないものを抽出
    For Each item In arrB
        If Not dict.Exists(item) Then
            Debug.Print "差分(Bのみ)=" & item
        End If
    Next item
End Sub
VB
  • 結果: 差分(Bのみ)=ぶどう

テンプレ3:共通部分を抽出

Sub Dict_Diff_Common()
    Dim arrA As Variant, arrB As Variant
    arrA = Array("りんご", "みかん", "バナナ")
    arrB = Array("りんご", "ぶどう")

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

    ' Aの要素を登録
    For Each item In arrA
        dict(item) = True
    Next item

    ' 共通部分を抽出
    For Each item In arrB
        If dict.Exists(item) Then
            Debug.Print "共通=" & item
        End If
    Next item
End Sub
VB
  • 結果: 共通=りんご

テンプレ4:シートの差分抽出(顧客リスト)

Sub Dict_Diff_Sheet()
    Dim ws As Worksheet: Set ws = Worksheets("Data")
    Dim lastRowA As Long: lastRowA = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim lastRowB As Long: lastRowB = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    Dim arrA As Variant: arrA = ws.Range("A2:A" & lastRowA).Value
    Dim arrB As Variant: arrB = ws.Range("B2:B" & lastRowB).Value

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

    ' B列の顧客を登録
    For r = 1 To UBound(arrB, 1)
        dict(arrB(r, 1)) = True
    Next r

    ' A列にあってB列にない顧客を抽出
    Dim i As Long: i = 2
    For r = 1 To UBound(arrA, 1)
        If Not dict.Exists(arrA(r, 1)) Then
            ws.Cells(i, 3).Value = arrA(r, 1) ' C列に出力
            i = i + 1
        End If
    Next r
End Sub
VB
  • ポイント:
    • A列とB列の顧客リストを比較。
    • 差分をC列に出力。

例題で練習

'例1:AにあってBにない差分
Sub Example1()
    Dict_Diff_A_NotInB
End Sub

'例2:BにあってAにない差分
Sub Example2()
    Dict_Diff_B_NotInA
End Sub

'例3:共通部分を抽出
Sub Example3()
    Dict_Diff_Common
End Sub

'例4:シートの顧客リスト差分
Sub Example4()
    Dict_Diff_Sheet
End Sub
VB

初心者向けポイント

  • Existsで存在判定 → 差分処理の基本。
  • AにあってBにない/BにあってAにない/共通 → 3パターンを覚える。
  • シートデータも同じ考え方 → Rangeを配列に読み込んで比較。
  • 大量データでも高速 → 数万件でも一瞬で差分抽出可能。

👉 この「Dictionary高速テンプレ(差分)」を覚えておけば、Excel VBAで リスト比較・差分抽出・更新チェック を高速に実務へ応用できます。

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