Sub VanToan1996()
Dim i&, j&, lr&, R&, n&, m&
Dim Arr(), KQ(), S, Tong()
Dim Dic As Object, Key
Dim Ws As Worksheet
Set Ws = Sheet1
lr = Ws.Cells(10000, "B").End(3).Row
Arr = Ws.Range("B4:F" & lr).Value
R = UBound(Arr)
ReDim KQ(1 To R * 2, 1 To 2): ReDim Tong(1 To R * 2)
Set Dic = CreateObject("Scripting.Dictionary")
For i = 1 To R
Key = Arr(i, 1)
If Not Dic.exists(Key) Then Dic(Key) = i Else Dic(Key) = Dic(Key) & "," & i
Next i
For Each Key In Dic.Keys
j = j + 1: KQ(j, 1) = Key
S = Split(Dic(Key), ",")
If UBound(S) > 0 Then
n = j
For m = 0 To UBound(S)
If Arr(S(m), 2) <> Empty Or Arr(S(m), 3) <> Empty Or Arr(S(m), 4) <> Empty Then
n = n + 1
KQ(n, 1) = " -" & Arr(S(m), 2) & "x" & Arr(S(m), 3) & "x" & Arr(S(m), 4) & " =" & Arr(S(m), 5)
Else
n = n + 1
KQ(n, 1) = Arr(S(m), 5)
End If
KQ(j, 2) = KQ(j, 2) + Arr(S(m), 5)
Next m
j = n
ElseIf UBound(S) = 0 Then
If Arr(S(0), 2) <> Empty Or Arr(S(0), 3) <> Empty Or Arr(S(0), 4) <> Empty Then
n = j + 1
KQ(n, 1) = " -" & Arr(S(0), 2) & "x" & Arr(S(0), 3) & "x" & Arr(S(0), 4) & " =" & Arr(S(0), 5)
KQ(j, 2) = KQ(j, 2) + Arr(S(0), 5)
Else
KQ(j, 2) = Arr(S(0), 5): n = j
End If
j = n
End If
Next
If j Then
Ws.Range("K4").Resize(10000, 2).ClearContents
Ws.Range("K4").Resize(j, 2) = KQ
End If
Set Dic = Nothing
End Sub