Maybe with a helper column to get the correspondent last price

ABCDE1 Qty Bought/Sold Price Bought/Sold Price Result2 20600 0,24 0,24 12103 8000 0,38 0,384 -20600 0,4 0,245 -8000 0,35 0,386 8000 0,37 0,377 2000 0,28 0,288 2500 0,26 0,269 -8000 0,4 0,37

Formula in C2 copied down

=IF(A2>0,B2,LOOKUP(2,1/(A$1:A1=-A2),B$1:B1))

Formula in E2

=SUMPRODUCT(A2:A9,C2:C9)

Note that changing the ranges to the original data setup (ignoring the added last row of data)

=SUMPRODUCT(A2:A8,C2:C8)

we get 4170 as desired

M.

## Like this thread? Share it with others