i didnt quite follow all of that but with using your e.g.
try this in D4

=if(C1=A2,B2,"")