I think this can be done just with formulas but your explanation is very difficult to understand, and the formulas you are using in your workbook do not make it any easier.

I have compared your first paragraph to your workbook. The first problem is that the same coupon code appears multiple times in "Buy". So how do you determine which row applies to the "Sell" row?

Second, it is completely unclear what your business rules are. If you could describe what you are actually trying to accomplish without diving down into Excel formulas it might be easier. For example, it is completely unclear why a calculation of weight would have anything to do with a coupon. In your situation what does "coupon" mean? If you hired me as a consultant I would say throw away your Excel workbook and tell me in detail what your requirements are so we can build the appropriate tool.

I can't figure out the next paragraph at all.

As a side note it is unnecessary to use

=SUM(G2-H2)

as seen in Buy!I2. Instead, use

=G2-H2