Dim rngAddress As String, Dic As Object
Function TongTon(ByVal rngNhap As Range, ByVal rngTon As Variant) As Long
Dim j&, tmp$, S
If rngAddress <> rngNhap.Address Then
rngAddress = rngNhap.Address
Call CreateDic(rngNhap.Value2)
End If
tmp = rngTon(1, 1)
S = Split(rngTon(1, 2), "|")
For j = 0 To UBound(S)
TongTon = TongTon + Dic.Item(tmp & "#" & S(j))
Next j
End Function
Private Sub CreateDic(ByVal sArr As Variant)
Dim sRow&, i&, j&, tmp$, S
Set Dic = CreateObject("scripting.dictionary")
sRow = UBound(sArr)
For i = 1 To sRow
tmp = sArr(i, 1)
S = Split(sArr(i, 2), ",")
For j = 0 To UBound(S)
iKey = tmp & "#" & S(j)
Dic.Item(iKey) = Dic.Item(iKey) + sArr(i, 3)
Next j
Next i
End Sub