I am trying to write a Conditional Loop for the following spreadsheet. Right now I have a series of buy/sell trades and I only want the Profit/loss to be calculated when the buy/sells for each stock becomes flat i.e. B=S. I also need my program to be able to identify the unique stock symbols. Below is a sample of the spreadsheet I am working with. If anyone has any help/suggestions or has done a similar program, please respond. Thank you. Below are the functions I have been using.

time symbol price buy_sell qty Position Net Position P/L
2/7/2011 9:32 ZAN 181.21 B 300 300 300 0
2/7/2011 9:36 ZAN 181 S 300 -300 0 63
2/7/2011 9:43 ZAN 181.23 B 400 400 400 0
2/7/2011 10:10 ZAN 181.21 S 400 -400 0 8
2/7/2011 10:18 ZAN 180.25 S 200 -200 -200 0
2/7/2011 10:19 ZAN 180.16 B 200 200 0 -18
2/7/2011 10:31 ZAN 180.23 B 400 400 400 0
2/7/2011 10:32 ZAN 180.15 S 400 -400 0 32

Option Explicit

Function Position(BS, qty)
If BS = "B" Then Position = qty
If BS = "S" Then Position = -qty

End Function

Function PL(NP, P01, P02, P1, P2)


If NP = 0 Then PL = P01 * P1 + P02 * P2 Else PL = 0

End Function