Type文(構造体)の定義と使い方

Type ステートメントで複数のフィールドをまとめたユーザー定義型(構造体)を作れます。

VBA — Type文の定義と使用
Option Explicit

' ── Type定義(標準モジュールの先頭に記述)──
Public Type Product
    Id    As Long
    Name  As String
    Price As Long
    Stock As Long
End Type

' ── 使用例 ──
Sub TypeExample()
    Dim p As Product
    p.Id    = 1
    p.Name  = "ノートPC"
    p.Price = 89800
    p.Stock = 10

    Debug.Print p.Name & " ¥" & p.Price  ' → ノートPC ¥89800

    ' 配列にも使える
    Dim products(1 To 3) As Product
    products(1).Id   = 1
    products(1).Name = "商品A"
End Sub

⚠️ Type文の制限

Type内にオブジェクト型(Workbook等)を持たせることはできません(Object型は可)。また、Type変数同士の直接代入(p1 = p2)でコピーはできますが、参照渡しにはなりません。

Collection vs Dictionary

機能CollectionDictionary
参照設定不要(組み込み)Microsoft Scripting Runtime が必要(またはCreateObject)
キー検索あり(文字列キーのみ)あり(文字列・数値等)
キー存在確認On Error Resume Next で確認(煩雑).Exists(key) で簡単に確認
値の上書き不可(削除して再追加)可(.Item(key) = value
キー一覧取得不可.Keys で配列取得

Scripting.Dictionary の操作

VBA — Dictionary の基本操作
Option Explicit

Sub DictionaryBasics()
    ' レイトバインディング(参照設定不要)
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    ' ── 追加 ──
    dict.Add "apple",  100
    dict.Add "banana", 80
    dict.Add "cherry", 150

    ' ── キー存在確認 ──
    If dict.Exists("apple") Then
        Debug.Print "apple: " & dict("apple")   ' → 100
    End If

    ' ── 値の更新 ──
    dict("apple") = 120   ' キーが存在すれば上書き

    ' ── キー・値の一覧 ──
    Dim key As Variant
    For Each key In dict.Keys
        Debug.Print key & " = " & dict(key)
    Next key

    ' ── 削除 ──
    dict.Remove "banana"
    dict.RemoveAll   ' 全削除

    ' ── カウント ──
    Debug.Print dict.Count   ' → 0

    Set dict = Nothing
End Sub

JavaのHashMapとの対比

操作Java (HashMap)VBA (Dictionary)
生成new HashMap<String,Integer>()CreateObject("Scripting.Dictionary")
追加map.put("key", value)dict.Add "key", value
取得map.get("key")dict("key") または dict.Item("key")
存在確認map.containsKey("key")dict.Exists("key")
削除map.remove("key")dict.Remove "key"
キー一覧map.keySet()dict.Keys
値一覧map.values()dict.Items
件数map.size()dict.Count

Type + Dictionary の組み合わせパターン

DictionaryのValueにType構造体を格納することで、複雑なデータを管理できます。

VBA — Type + Dictionary の組み合わせ
Option Explicit

Public Type Employee
    Name   As String
    Dept   As String
    Salary As Long
End Type

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

    ' Type変数をVariantとしてDictionaryに格納
    Dim emp As Employee
    emp.Name   = "田中 太郎"
    emp.Dept   = "開発"
    emp.Salary = 500000
    dict.Add "E001", emp

    emp.Name   = "鈴木 花子"
    emp.Dept   = "営業"
    emp.Salary = 450000
    dict.Add "E002", emp

    ' 取得・表示
    Dim id As Variant
    For Each id In dict.Keys
        Dim e As Employee
        e = dict(id)
        Debug.Print id & ": " & e.Name & " / " & e.Dept
    Next id

    Set dict = Nothing
End Sub

まとめ

Type 文でデータをまとめ、Dictionary でキー検索を実現する組み合わせは、VBAでのデータ管理の定番パターンです。参照設定なしで使えるレイトバインディング(CreateObject)は配布物にも有効です(詳細はPART 08で解説)。

次の章では…

PART 07 では クラスモジュール を解説します。Property Get/Let によるカプセル化とJavaクラスとの対比を丁寧に紹介します。

→ PART 07 — クラスモジュールへ