Sub AnyThing() Dim lastrow_1 As Long, counter As Long
Dim lastrow_2 As Long, key As Variant
Dim sh1 As Worksheet, sh2 As Worksheet
Dim rng1, rng2 As Range, p As Variant
Dim dict As Object
Set sh1 = Sheets("SHEET1")
Set sh2 = Sheets("SHEET2")
sh2.Range("I3").Resize(1000, 3).ClearContents
lastrow_1 = sh1.Cells(sh1.Rows.Count, "B").End(3).Row
lastrow_2 = sh1.Cells(sh2.Rows.Count, "C").End(3).Row
Set rng1 = sh1.Range("B3:E" & lastrow_1)
Set rng2 = sh2.Range("C2:E" & lastrow_2)
Set dict = CreateObject("Scripting.Dictionary")
For Each p In rng1.Columns(2).Cells
If Not dict.Exists(p.Value & "," & p.Offset(, 1)) Then
dict.Add p.Value & "," & p.Offset(, 1), p.Offset(, 2)
Else
dict(p.Value & "," & p.Offset(, 1)) = _
dict(p.Value & "," & p.Offset(, 1)) + p.Offset(, 2)
End If
Next p
'===============================
For Each p In rng2.Columns(2).Cells
If Not dict.Exists(p.Value & "," & p.Offset(, 1)) Then
dict.Add p.Value & "," & p.Offset(, 1), p.Offset(, 2)
Else
dict(p.Value & "," & p.Offset(, 1)) = _
dict(p.Value & "," & p.Offset(, 1)) + p.Offset(, 2)
End If
Next p
'==============================
counter = 2
With sh2
For Each key In dict.Keys
counter = counter + 1
.Cells(counter, "K").Resize(1, 2) = Split(key, ",")
.Cells(counter, "O") = dict(key)
Next key
End With
dict.RemoveAll: Set dict = Nothing
Set sh1 = Nothing: Set sh2 = Nothing
Set rng1 = Nothing: Set rng2 = Nothing
End Sub