Sub XYZ()
Dim sRow&, sR&, i&, sCol&
Dim aTK(), aPS(), res(), arr, dic As Object
Set dic = CreateObject("Scripting.Dictionary")
With Sheet2
aPS = .Range("A3:D" & .Range("A" & Rows.Count).End(xlUp).Row).Value
End With
With Sheet1
aTK = .Range("A3:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value
End With
sRow& = UBound(aTK)
ReDim res(1 To sRow, 1 To 50)
For i = 1 To sRow
dic.Item(aTK(i, 1)) = Array(i, 0)
dic.Item(aTK(i, 1) & "|" & aTK(i, 2)) = ""
Next i
sR& = UBound(aPS)
For i = 1 To sR
If dic.exists(aPS(i, 1)) Then
If Not dic.exists(aPS(i, 1) & "|" & aPS(i, 2)) Then
arr = dic.Item(aPS(i, 1))
arr(1) = arr(1) + 1
res(arr(0), arr(1) * 2 - 1) = aPS(i, 2)
res(arr(0), arr(1) * 2) = aPS(i, 3) + aPS(i, 4)
dic.Item(aPS(i, 1)) = arr
dic.Item(aPS(i, 1) & "|" & aPS(i, 2)) = ""
If sCol < arr(1) Then sCol = arr(1)
End If
End If
Next i
Sheet1.Range("D3").Resize(sRow, sCol * 2) = res
End Sub