Sub SoSanh()
Dim i&, j&, k&, Lr1&, Lr2&, n&, m&
Dim Arr1(), Arr2(), KQ(), KETQUA()
Dim Dic As Object
With Sheet1
Lr1 = .Cells(Rows.Count, 1).End(xlUp).Row
Arr1 = .Range("A2:C" & Lr1).Value
R1 = UBound(Arr1)
End With
With Sheet2
Lr2 = .Cells(Rows.Count, 1).End(xlUp).Row
Arr2 = .Range("A2:C" & Lr2).Value
R2 = UBound(Arr2)
End With
ReDim KQ(1 To R1 + R2, 1 To 5)
Set Dic = CreateObject("scripting.dictionary")
For i = 1 To R1
Keys = Application.Substitute(Trim(Arr1(i, 1)), "/GNT", "")
If Not Dic.Exists(Keys) Then
t = t + 1
Dic.Add (Keys), t
KQ(t, 1) = Format(Keys, "0000#")
KQ(t, 2) = Arr1(i, 2)
KQ(t, 3) = Arr1(i, 3)
Else
k = Dic.Item(Keys)
KQ(k, 2) = Arr1(i, 2)
KQ(k, 3) = KQ(k, 3) + Arr1(i, 3)
End If
KQ(t, 5) = KQ(t, 3) - KQ(t, 4)
Next i
For j = 1 To R2
Temp = Trim(Arr2(j, 1))
If Not Dic.Exists(Temp) Then
t = t + 1
KQ(t, 1) = Format(Temp, "0000#")
KQ(t, 2) = Arr2(j, 2)
KQ(t, 4) = Arr2(j, 3)
Else
k = Dic.Item(Temp)
KQ(k, 2) = Arr2(j, 2)
KQ(k, 4) = KQ(k, 4) + Arr2(j, 3)
KQ(k, 5) = KQ(k, 3) - KQ(k, 4)
End If
KQ(t, 5) = KQ(t, 3) - KQ(t, 4)
Next j
ReDim KETQUA(1 To t, 1 To 5)
For n = 1 To t
If KQ(n, 3) <> KQ(n, 4) Then
Z = Z + 1
For m = 1 To 5
KETQUA(Z, m) = KQ(n, m)
Next m
KETQUA(Z, 1) = Format(KETQUA(Z, 1), "0000#")
End If
Next n
If Z Then
Sheet3.Cells(2, 1).Resize(Z + 5, 5).ClearContents
Sheet3.Cells(2, 1).Resize(Z, 5) = KETQUA
End If
Set Dic = Nothing
MsgBox " Xong"
End Sub