***************************************************************
配列をDictionary(辞書)に変換する
1次元配列の場合、配列中の値をKeyとした辞書を作成
2次元配列の場合、1列目をKey、2列めをItemとした辞書を作成
【引数】DataAry :データ配列(1次元or2次元)
IgnoreNullStrKey:Keyに空文字があった場合に除去するオプション
***************************************************************
Public Function fArray_to_Dictionary(DataAry As Variant, _
Optional IgnoreNullStrKey As Boolean = False) As Scripting.Dictionary
Dim Dic As Scripting.Dictionary
Set Dic = New Scripting.Dictionary
Dim Row_L As Long
Dim Row_U As Long
Dim Col_L As Long
Dim Col_U As Long
Call fArray_Lbound_Ubound(DataAry, Row_L, Row_U, Col_L, Col_U)
Dim CntDim As Long
CntDim = fArray_DimCount(DataAry)
Select Case CntDim
Case 1
Dim i As Long
For i = Row_L To Row_U
If Dic.Exists(DataAry(i)) = False Then
Call Dic.Add(DataAry(i), Empty)
End If
Next
Case 2
'- 1列目をKey列、2列目をItem列に設定
Dim Col_K As Long
Dim Col_I As Long
Col_K = Col_L
Col_I = Col_K + 1
'- 1列だけの場合
If Col_L = Col_U Then
For i = Row_L To Row_U
If Dic.Exists(DataAry(i, Col_K)) = False Then
Call Dic.Add(DataAry(i, Col_K), Empty)
End If
Next
'- 複数列の場合
Else
For i = Row_L To Row_U
If Dic.Exists(DataAry(i, Col_K)) = False Then
Call Dic.Add(DataAry(i, Col_K), DataAry(i, Col_I))
End If
Next
End If
Case Else
End Select
'- EmptyはKeyになり得ないので削除
'+ Key:0,"" があった場合に誤って除去されてしまうので、Loopで回して判定する
Dim Key As Variant
For Each Key In Dic.Keys
If IsEmpty(Key) = True Then
Call Dic.Remove(Key)
End If
Next
'- ""は設定によって削除
If IgnoreNullStrKey = True Then
If Dic.Exists(vbNullString) = True Then
Call Dic.Remove(vbNullString)
End If
End If
Set fArray_to_Dictionary = Dic
Set Dic = Nothing
End Function