Put this on E1 and copied down
=IFERROR(VLOOKUP(F1,CHOOSE({1\2},$B$1:$B$9,$A$1:$A$9),2,0),"")
And put this on F1 and copied down
=IFERROR(INDEX($B$1:$B$9,AGGREGATE(15,6,(ROW($B$1:$B$9)-MIN(ROW($B$1:$B$9))+1)/($B$1:$B$9<>0),ROW(A1))),"")
Hope this works
Bookmarks