Function fncGetValue(vrData As Range, vrItem As Range, vrKind As Range)
Dim ret As Double '戻り値
ret = 0
Dim i As Integer
'実績データの領域チェック
If vrData.Columns.Count < 2 Then
fncGetValue = "#Data Err"
Exit Function
End If
'科目ディクショナリ
'↓Windows.Scripting.Runtimeを参照設定に追加していない場合はこちら
'Dim dicKinds As Object '科目ディクショナリ
'Set dicKinds = CreateObject("Scripting.Dictionary")
'↑
'↓Windows.Scripting.Runtimeを参照設定に追加している場合はこちら
Dim dicKinds As Dictionary '科目ディクショナリ
Set dicKinds = New Dictionary
'↑
'商材セルの結合サイズから科目ディクショナリを作成
For i = 1 To vrItem.MergeArea.Rows.Count
If vrItem.Cells(i, 2) <> "粗利" Then
dicKinds.Add vrItem.Cells(i, 2).Value, 0
End If
Next
Dim strKind As String '実績データの科目
Dim strVal1 As String '実績データのA列セル値
Dim strVal2 As String '実績データのB列セル値
'対象範囲をループ処理
For i = 1 To vrData.Rows.Count
'値取得
strVal1 = vrData.Cells(i, 1).Value 'A列
strVal2 = vrData.Cells(i, 2).Value 'B列
If dicKinds.Exists(strVal1) = True Then
'科目ディクショナリにある:「科目」
strKind = strVal1
Else
'科目ディクショナリにない:「商材」
'今回の抽出対象化判定
If strKind = vrKind.Value And strVal1 = vrItem.Value Then
''対象科目の対象商材なら戻り値に加算
'ret = ret + Val(strVal2)
'見つけたら値を返して終了
ret = Val(strVal2)
Exit For
End If
End If
Next
fncGetValue = ret
End Function