Hi Mahaveer ,
If you don't mind using VBA , try this :
Public Sub Calculate_FIFO_Profit()
Dim Purchase_Qty As Variant, Purchase_Rate As Variant, Selling_Qty As Variant, Selling_Rate As Variant
Dim Counter As Integer
' Purchase_Qty is a named range referring to =Sheet2!$C$3:INDEX(Sheet2!$C:$C,COUNTA(Sheet2!$C:$C)+1)
' Purchase_Rate is a named range referring to =Sheet2!$D$3:INDEX(Sheet2!$D:$D,COUNTA(Sheet2!$D:$D)+1)
' Selling_Qty is a named range referring to =Sheet2!$G$3:INDEX(Sheet2!$G:$G,COUNTA(Sheet2!$G:$G)+1)
' Selling_Rate is a named range referring to =Sheet2!$H$3:INDEX(Sheet2!$H:$H,COUNTA(Sheet2!$H:$H)+1)
Purchase_Qty = Range("Purchase_Qty").Value
Purchase_Rate = Range("Purchase_Rate").Value
Selling_Qty = Range("Selling_Qty").Value
Selling_Rate = Range("Selling_Rate").Value
Counter = 1
i = LBound(Selling_Qty, 1)
Sell_Qty = Selling_Qty(i, 1)
Pur_Qty = Purchase_Qty(Counter, 1)
Do
If Sell_Qty > Pur_Qty Then
Profit = Profit + (Selling_Rate(i, 1) - Purchase_Rate(Counter, 1)) * Pur_Qty
Sell_Qty = Sell_Qty - Pur_Qty
Counter = Counter + 1
Pur_Qty = Purchase_Qty(Counter, 1)
Else
Profit = Profit + (Selling_Rate(i, 1) - Purchase_Rate(Counter, 1)) * Sell_Qty
Pur_Qty = Pur_Qty - Sell_Qty
i = i + 1
If i > UBound(Selling_Qty, 1) Then Exit Do
Sell_Qty = Selling_Qty(i, 1)
End If
Loop
MsgBox Profit
End Sub
Please note that there is no date checking to see whether the quantity sold as on any date is actually less than or equal to the total quantity purchased till that date.
Narayan