連想配列
連想配列とは、添字(キー)に文字列を使用することができる配列です。
    VBA で、連想配列を使用するには、CreateObject 関数の引数に「Scripting.Dictionary」を指定するか、参照設定「Microsoft Scripting Runtime」を設定します。
(※ コレクション(Collection)オブジェクト を簡単な連想配列として使用することも出来ます。)
Dictionary オブジェクト
    <CreateObject 関数を使用する場合>
    Dim myDic As Object
    Set myDic = CreateObject(“Scripting.Dictionary”)
    <参照設定を行う場合>
    Dim myDic As Dictionary
    Set myDic = New Dictionary
引数・戻り値
- myDic ・・・ Dictionary オブジェクト
 
Add メソッド
連想配列に新規の「キー」と「値」のセットを追加します。
object.Add key, item
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 - key ・・・ 追加する「キー」となる文字列を指定します。
 - item ・・・ 追加する「値」を指定します。
 
Exists メソッド
指定した「キー」が、連想配列に存在するかどうかを確認します。
    <取得>
    bool = object.Exists key
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 - key ・・・ 検索する「キー」を指定します。連想配列に存在する場合は、True 、存在しない場合は、False を返します。
 
Items メソッド
連想配列のすべての「値」を取得します。
    <取得>
    array = object.Items
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 - array  ・・・  連想配列の「値」のみを配列として取得したもの
(例)連想配列 orgArray
orgArray(“first”) = 100、orgArray(“second”) = 150、orgArray(“third”) = 200 の場合、
array(0) = 100、array(1) = 150、array(3) = 200 
Keys メソッド
連想配列のすべての「キー」を取得します。
    <取得>
    array = object.Keys
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 - array  ・・・  連想配列の「キー」のみを配列として取得したもの
(例)連想配列 orgArray
orgArray(“first”) = 100、orgArray(“second”) = 150、orgArray(“third”) = 200 の場合、
array(0) = “first”、array(1) = “second”、array(3) = “third” 
Remove メソッド
連想配列の「キー」と「値」のセットを削除します。
object.Remove key
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 - key ・・・ 削除する「キー」を指定します。「キー」が見つからなかった場合はエラーが発生します。
 
RemoveAll メソッド
連想配列のすべての「キー」と「値」を削除します。
object.RemoveAll
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 
Count プロパティ
連想配列の項目数を取得します。
    <取得>
    object.Count
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 
Item プロパティ
連想配列で、「キー」に関連付けられた「値」を取得します。または、連想配列に新規の「キー」と「値」のセットを追加します。
    <取得>
    value = object[ .Item ](key)
    <設定>
    object[ .Item ](key) = newitem
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 - key ・・・ 取得または設定する項目に関連付けられている「キー」を指定します。
 - newitem ・・・ 「キー」(key)に関連付ける「値」を指定します。
 
Key プロパティ
指定した「キー」を新しい「キー」に置き換えます。
    <設定>
    object.Key(key) =  newkey
引数・戻り値
- object ・・・ 対象となる Dictionary オブジェクトを指定します。
 - key ・・・ 変更する「キー」を指定します。
 - newkey ・・・ key で指定した「キー」と置き換える新規の「キー」を指定します。
 
VBA で、連想配列を使用する例
サンプルVBAソース1
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40  | 
						Sub Sample_Dictionary1()     '重複を削除して表示する     Dim myDic As Object     Dim myKey As Variant     Dim myRng As Range     Dim i As Long     Dim c As Variant     Dim v As Variant     Dim myStr As String     Set myDic = CreateObject("Scripting.Dictionary")     With Worksheets("Sheet1")         Set myRng = Range("A1").CurrentRegion         For Each c In myRng             myKey = c.Value             If Not myDic.Exists(myKey) Then                 myDic.Add myKey, ""             End If         Next c         i = 1         For Each v In myDic.Keys             myStr = myStr & i & vbTab & v & vbCrLf             i = i + 1         Next v     End With     MsgBox myStr     Set myDic = Nothing End Sub  | 
					
実行結果
サンプルVBAソース2
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  | 
						Sub Sample_Dictionary2()     '参照設定「Microsoft Scripting Runtime」     '重複したデータの値を合計して表示     Dim myDic As Dictionary     Dim myKey As Variant     Dim i As Long     Dim v As Variant     Dim myStr As String     Set myDic = New Dictionary     With Worksheets("Sheet2")         For i = 1 To .Cells(.Cells.Rows.Count, 1).End(xlUp).Row             myKey = Cells(i, 1).Value             If myDic.Exists(myKey) Then                 myDic(myKey) = myDic(myKey) + .Cells(i, 2)             Else                 myDic.Add myKey, .Cells(i, 2)             End If         Next i         i = 1         For i = 1 To myDic.Count             myStr = myStr & myDic.Keys(i - 1) & vbTab & _                             myDic.Items(i - 1) & vbCrLf     '← myDic (myDic.Keys(i - 1)) & vbCrLf でもよい         Next i     End With     MsgBox myStr     Set myDic = Nothing End Sub  |