Sub UNIQUE4()
Dim a As Variant, i As Long, o As Variant, d As Object, b As Variant, f As Object, Lr As Long, Lc As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")
a = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
b = .Range("B2:B" & .Range("B" & Rows.Count).End(xlUp).Row)
Set d = CreateObject("Scripting.Dictionary")
Set f = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(a, 1)
d(a(i, 1)) = d(a(i, 1))
f(b(i, 1)) = f(b(i, 1))
Next i
Sheets("Sheet2").Range("A2").Resize(d.Count) = Application.Transpose(Array(d.Keys))
Sheets("Sheet2").Range("B1").Resize(, f.Count) = f.Keys
End With
With Sheets("Sheet2")
Lr = .Range("A" & Rows.Count).End(xlUp).Row
Lc = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range("B2").FormulaR1C1 = "=SUMIFS(Sheet1!R2C3:R" & Lr & "C3,Sheet1!R2C1:R" & Lr & "C1,Sheet2!RC1,Sheet1!R2C2:R" & Lr & "C2,Sheet2!R1C)"
.Range("B2").AutoFill Destination:=.Range("B2:B" & Lr), Type:=xlFillDefault
.Range("B2:B" & Lr).AutoFill Destination:=.Range(.Cells(2, 2), .Cells(Lr, Lc)), Type:=xlFillDefault
Range(.Cells(2, 2), .Cells(Lr, Lc)).Value = Range(.Cells(2, 2), .Cells(Lr, Lc)).Value
End With
Application.ScreenUpdating = True
End Sub