Excel VBA 入門講座

Excel VBA Tips Excel VBA 入門講座

連想配列(Dictionaryオブジェクト使用)

一般的な配列は、添え字に数値を使用します。そうではなく、文字列の要素を文字の添え字で操作できるような配列を 連想配列と呼びます。例えば商品名と価格のような関係です。

連想配列を使用する場合、Dictionaryオブジェクトを使用する方法とCollectionオブジェクトを使用する方法とがあります。ここではDictionaryオブジェクトを使用するご紹介します。

Dictionaryオブジェクトを使用する方法

Dictionaryオブジェクトにて連想配列を使用する場合、以下の通り宣言します。


Dim 連想配列名 As Object
Set 連想配列名 = CreateObject(“Scripting.Dictionary”)

Dictionaryオブジェクトには以下のメソッドとプロパティが用意されています。

メソッド説明
AddDictionary オブジェクトに新しいキーとアイテムを追加します
ExistsDictionary オブジェクト内に指定したキーが存在する場合True、存在しない場合Falseを返します
ItemsDictionary オブジェクト内のすべてのアイテムの配列を返します
KeysDictionary オブジェクト内のすべてのキーの配列を返します
RemoveDictionary オブジェクトから、指定したキーとアイテムを削除します
RemoveAllDictionary オブジェクト内のすべてのキーとアイテムを削除します
プロパティ説明
CompareModeDictionary オブジェクト内でキーを比較するために大文字と小文字を区別するか指定します
CountDictionary オブジェクト内のキー、アイテムの数を返します
ItemDictionary オブジェクト内のアイテムの値を設定するか返します

連想配列に値を追加する場合はAddメソッドを使用してキーとアイテムを追加します。 以下のサンプルでは商品名と値段をオブジェクトに追加、商品の値段を表示させています。


Sub SetArray()


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

    objDic.Add "商品A", 1000
    objDic.Add "商品B", 2000
    objDic.Add "商品C", 3500
    
    MsgBox objDic.Item("商品A")
    MsgBox objDic.Item("商品B")
    MsgBox objDic.Item("商品C")

End Sub

以下のサンプルでは商品名と値段をセルより取得してオブジェクトにセット後、連想配列の内容を表示しています。

セルの内容
商品表1

Sub SetArray()

    Dim strProductName As String
    Dim curProductPrice As Currency
    
    Dim varKeys As Variant
    
    Dim strMsg As String
    Dim i As Integer
    
    Dim objDic As Object
    Set objDic = CreateObject("Scripting.Dictionary")

    For i = 2 To 4
        strProductName = Worksheets("Sheet1").Cells(i, 1).Value
        curProductPrice = Worksheets("Sheet1").Cells(i, 2).Value
        objDic.Add strProductName, curProductPrice
    Next

    varKeys = objDic.Keys
    For i = 0 To objDic.Count - 1
        strMsg = strMsg & varKeys(i) & " : " & objDic.Item(varKeys(i)) & vbCrLf
    Next i

    MsgBox (strMsg)

End Sub

上記の例では一旦Keysの値を配列に代入してから内容を表示させていますが、For Eachステートメントを使用すると不要となります。


Sub SetArray()

    Dim strProductName As String
    Dim curProductPrice As Currency
    
    Dim strMsg As String
    Dim i As Integer
        
    Dim objDic As Object
    Set objDic = CreateObject("Scripting.Dictionary")

    For i = 2 To 4
        strProductName = Worksheets("Sheet1").Cells(i, 1).Value
        curProductPrice = Worksheets("Sheet1").Cells(i, 2).Value
        objDic.Add strProductName, curProductPrice
    Next
    
    For Each v In objDic
       strMsg = strMsg & v & " : " & objDic.Item(v) & vbCrLf
    Next v
    
    MsgBox (strMsg)
        
End Sub

Dictionaryオブジェクトには重複してキーを追加することができません。以下のセルの場合、商品Cが重複しているので追加時にエラーとなります。

セルの内容
商品表2

このような場合、Existsメソッドを使用して既にキーが存在するか判定してから追加するようにします。


Sub SetArray()

    Dim strProductName As String
    Dim curProductPrice As Currency
    
    Dim strMsg As String
    Dim i As Integer

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

    For i = 2 To 5
        strProductName = Worksheets("Sheet1").Cells(i, 1).Value
        curProductPrice = Worksheets("Sheet1").Cells(i, 2).Value
        If objDic.Exists(strProductName) = False Then
            objDic.Add strProductName, curProductPrice
        End If
    Next
    
    For Each v In objDic
       strMsg = strMsg & v & " : " & objDic.Item(v) & vbCrLf
    Next v
    
    MsgBox (strMsg)
    
End Sub
Excel VBA Tips Excel VBA 入門講座 TOP