FIFO matching sale qty to purchase qty

pulvi2003

New Member
Joined
Jul 3, 2016
Messages
7
I have a worksheet with columns as follows :
A B C D E F
item batch date qty rate amount
berries p-1 11/11/2010 2300 39.59 91065.97
berries p-2 24/12/2013 2000 22.60 45200.00
berries p-3 26/12/2013 700 22.50 15750.00
berries s-1 01/06/2017 -1000 89.87 89865.65
berries s-2 02/06/2017 -1000 92.02 92020.00
berries s-3 24/08/2017 -1000 93.02 93020.00
berries s-4 26/12/2017 -1000 95.83 95830.00
berries s-5 28/12/2013 -1000 94.73 94730.00
carrots p-1 17/08/2016 1000 21.54 21540.00
carrots p-2 24/08/2016 5350 24.54 131289.00
carrots s-1 06/06/2017 - 850 26.00 22100.00
carrots s-2 09/06/2017 - 500 26.50 13250.00
The positive qty denotes purchase in batches p-1,p2... and -ve qty denotes sale in batches s-1,s2...I want to match the sale qty with purchase qty on FIFO (First in First out) basis and calculate profit / loss in each sale and the remaining stock after all sale adjusted, with its cost value What formula in excel to do the above calculations ?
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Does the batch code prefix of "p" indicate a purchase ?
And "s" indicate a sale ?

For the sale of berries s-1 on 1/6/17, are we meant to somehow identify which of the purchases of berries this came from ?

Can you tell us what the answers SHOULD be for your data sample ?
 
Upvote 0
If I'm correct, on every "sell" line, you want to figure out how many units to take from the first "buy" and how much they cost, how many from the second "buy" line and how much they cost, and so on, and give the total cost. Sell lines lower down would take into account any "sells" ahead of it. If that's true, that would be a really tricky formula. I've played around with it for a couple days now and could not work it out. It can be done with a User-Defined Function somewhat easier.

Open a copy of your workbook. Press Alt-F11 to open the VBA editor. From the menu, select Insert > Module. Paste the following code into the window that opens:
Code:
Public Function FIFOCost(ByVal MyRange As Range) As Variant
Dim i As Long, MyData As Variant, lr As Long, tot As Double, amt As Double, Sale As Double, w as Double

' Eric W, MrExcel.com, 1/11/2019

    Application.Volatile
    MyData = MyRange.Value
    lr = UBound(MyData)
    If Left(MyData(lr, 2), 1) <> "s" Then
        FIFOCost = ""
        Exit Function
    End If
    
    tot = 0
    For i = LBound(MyData) To lr - 1
        If LCase(Left(MyData(i, 2), 1)) = "s" And MyData(i, 1) = MyData(lr, 1) Then tot = tot - MyData(i, 4)
    Next i
    
    Sale = MyData(lr, 4) * -1
    For i = LBound(MyData) To lr - 1
        If LCase(Left(MyData(i, 2), 1)) = "p" And MyData(i, 1) = MyData(lr, 1) Then
            amt = MyData(i, 4)
            If tot > amt Then
                tot = tot - amt
            Else
                amt = amt - tot
                tot = 0
                w = IIf(amt > Sale, Sale, amt)
                FIFOCost = FIFOCost + w * MyData(i, 5)
                Sale = Sale - w
            End If
        End If
        If Sale = 0 Then Exit Function
    Next i

End Function
Now press Alt-Q to close the editor. Now you can use the following formula:

ABCDEFGH
1itembatchdateqtyrateamountFIFO
2berriesp-111/11/2010230039.5991065.97
3berriesp-212/24/2013200022.645200
4berriesp-312/26/201370022.515750
5berriess-16/1/2017-100089.8789865.6539590
6berriess-26/2/2017-100092.029202039590
7berriess-38/24/2017-100093.029302027697
8berriess-412/26/2017-100095.839583022600
9berriess-512/28/2017-100094.739473022530
10carrotsp-18/17/2016100021.5421540
11carrotsp-28/24/2016535024.54131289
12carrotss-16/6/2017-850262210018309
13carrotss-26/9/2017-50026.51325011820

<tbody>
</tbody>
Sheet8

Worksheet Formulas
CellFormula
G2=FIFOCost($A$2:$F2)

<tbody>
</tbody>

<tbody>
</tbody>



Hope this helps.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top