Option Explicit
Sub LocLech()
Dim i&, j&, Lr1&, Lr2&, t&, k&, R2&, R1&, z&
Dim Arr1(), Arr2(), KQ(), Res()
Dim Dic As Object
Dim Key, Tmp
Dim Ws As Worksheet, Sh1 As Worksheet, Sh2 As Worksheet
Set Sh1 = Sheets("DS1")
Lr1 = Sh1.Cells(1000000, 3).End(3).Row
Arr1 = Sh1.Range("C4:F" & Lr1).Value
R1 = UBound(Arr1)
Set Sh1 = Sheets("DS2")
Lr2 = Sh1.Cells(1000000, 3).End(3).Row
Arr2 = Sh1.Range("B5:F" & Lr2).Value
R2 = UBound(Arr2)
ReDim KQ(1 To R1 + R2, 1 To 9)
Set Dic = CreateObject("Scripting.Dictionary")
For i = 1 To R1
If Arr1(i, 1) <> Empty Then
Key = Arr1(i, 1) & "|" & Arr1(i, 2)
If Not Dic.Exists(Key) Then
t = t + 1: Dic.Add (Key), t
KQ(t, 2) = Arr1(i, 1)
KQ(t, 3) = Arr1(i, 2)
KQ(t, 4) = Arr1(i, 3)
KQ(t, 7) = Arr1(i, 4)
Else
k = Dic.Item(Key)
KQ(k, 4) = KQ(k, 4) + Arr1(i, 3)
KQ(k, 7) = KQ(k, 7) + Arr1(i, 4)
End If
End If
Next i
For i = 1 To R2
If Arr2(i, 1) <> Empty Then
Key = Arr2(i, 1) & "|" & Arr2(i, 2)
If Not Dic.Exists(Key) Then
t = t + 1: Dic.Add (Key), t
KQ(t, 2) = Arr2(i, 1)
KQ(t, 3) = Arr2(i, 2)
KQ(t, 5) = Arr2(i, 3)
KQ(t, 8) = Arr2(i, 5)
Else
k = Dic.Item(Key)
KQ(k, 5) = KQ(k, 5) + Arr2(i, 3)
KQ(k, 8) = KQ(k, 8) + Arr2(i, 5)
End If
End If
Next i
ReDim Res(1 To t, 1 To 9)
For i = 1 To UBound(KQ)
KQ(i, 6) = KQ(i, 4) - KQ(i, 5)
KQ(i, 9) = KQ(i, 7) - KQ(i, 8)
If KQ(i, 4) - KQ(i, 5) <> KQ(i, 7) - KQ(i, 8) Then
z = z + 1
Res(z, 1) = z
For j = 2 To 9
Res(z, j) = KQ(i, j)
Next j
End If
Next i
Set Ws = Sheets("Lech")
Ws.Range("A12").Resize(t, 9) = Res
Set Dic = Nothing
MsgBox " Done"
End Sub