Maybe, put this on D1 and copied down as necessary:
=IF(COUNTIF($A$1:$A$22,A1)<2,"",IF(COUNTIF($A$1:A1,A1)=COUNTIF($A$1:$A$22,A1),INDEX(A:A,SUMPRODUCT(MAX(($A$1:A1=A1)*ROW($A$1:A1)))),""))
And put this on E1 and copied down and cross:
=IF(SUMIFS(B$1:B$22;$A$1:$A$22;$D1)=0;"";SUMIFS(B$1:B$22;$A$1:$A$22;$D1))
Bookmarks