Fifo Method Of Stock Valuation

sujittalukde

Well-known Member
Joined
Jun 2, 2007
Messages
520
Also posted at http://www.ozgrid.com/forum/showthread.php?t=71862 but not geeting any reply So posting here.

have a file where I want to make a report which will put the stocks having positive balances along with their prices and values.

Stocks are to be valued on FIFO (First in first out) method ie when the stocks are sold it means it were sold from the first lot bought and so on.



Reporting is required for Shares Held For eg. for Stock A, Buy qtn is (50+50+100+<sell>+100) and sell qtn is 100 So total balance is Buy Qtn 300 - sell Qtn 100 = 200 Sell Qtn 100 will out of first 50 & then next 50 (First in so first out) balance held in hand is out of last two Qtn 100 purchases. The prices are corresponding to these two lots of 100 stocks.

Reporting will be for Stock held (lotwise) See Sheet 2 of the file attached.
Basis of price will be fixed ie no formula etc and corresponding to those lots.

Actually these data are copied from web qwery so formula etc are not required. For cost purpose, price and Total correponding the balnce qtn lot are relevant

In case more clarification required, do let me know. I will try to make them clear.
FIFO.xls
ABCDEFG
1StockDateActionQty.RateTotal
2A25-Nov-04Buy5070.33,546.70
3A20-Jul-05Buy50663,330.85
4A01-Sep-05Buy100828,275.97
5A23-Sep-05Sell10076.57,579.12
6A17-Oct-05Buy10073.87,448.38
7B03-Jan-05Buy200125.4925,325.16
8B12-Jan-05Sell20098.519,522.39
9BA05-Sep-06Sell30014342,485.23Qtn balance is negative so ignore this stock in reporting
10BC21-Dec-04Buy20284.75,745.33
11BC12-Jan-05Sell20248.64,927.18
12C21-Dec-04Buy30015.34,631.40Red font means buy sell matched
13C12-Jan-05Sell30017.75,262.12
14C17-Jan-05Buy20018.353,703.08
15C03-Mar-06Buy9725.552,508.38yellow background means these are in balance
16C26-Apr-06Buy10324.052,507.69and should be reported
17C05-Dec-06Buy10030.83,111.91
18C08-Mar-07Buy10032.953,327.18
19D15-Jan-07Buy100128.512,974.24
20E08-Mar-07Buy10064.656,527.36
21HH31-May-07Buy10062.56,310.48
22ID07-Dec-04Buy7050.853,591.59
23ID12-Jan-05Sell6068.654,081.87
24IDF30-Dec-04Buy10107.251,100.85
25IDF12-Jul-05Sell17112.81,888.13Qtn balance is negative so ignore this stock in reporting
Sheet1
 
Purchase of "A" = 50+50+100+100 (DAtewise) so if the first sell is 70 this means it has come from first 50 lot and balance 20 from second 50 lot. Total balnace will be then 50+50+100-70+100=230 (comprises 30 from second 50 lot, 100 from full 100 Lot (third purchase), finally, 100 from next 100 purchase lot)

You can ask any number of question i wont mind as I know this type of problems are very tricky and with out clearing doubts correct code cannot be written.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
no selling price need not be considered as because all this data I copy from online so no formula etc are needed. Only the balances in hand (lotwise) and rates and Total should be considered.

The logic to generate the report by the code could be thus:

1. The code will count the balances for each item.
2. Then it will search for lots for those balances Here the code has to match the sell qtn with the purchase qtn and adjust the sell qtn with the purchases made first ie those purchases made first will go out first
3. Then it will pick values for those balance quantities for rates and total.

In this process, for items whose balances will come to negative ie only sell qtn found and no purchase, the code will not report the same.

Please note that these steps are for your guidance only You have to make adjustments for Exceution of code as I am not aware of the steps that how the code will follow the steps.
 
Upvote 0
try this
if any bugs, post the details of erro(s) and I will reply tomorrow.
Code:
Sub test()
Dim a, i As Long, ii As Long, iii As Long, w(), x, y, z, n As Long
a = Sheets("Sheet1").Range("a1").CurrentRegion.Resize(,5).Value
With CreateObject("Scripting.Dictionary")
     .CompareMode = vbTextCompare
     For i = 2 To UBound(a,1)
          If Not dic.exists(a(i,1)) Then
               ReDim w(1 To 2, 1 To 1)
               For ii = 1 To 2 : w(ii,1) = a(i, ii + 3) : Next
               If a(i,3) = "Sell" Then w(1,1) = "Ignored"
               .add a(i,1), w
          Else
               w = .item(a(i,1))
               If w(1,1) <> "Ignored" Then
                    If a(i,3) = "Buy" Then
                         ReDim Preserve w(1 To 2, 1 To UBound(w,2) + 1)
                         z = UBound(w,2)
                         For ii = 1 To 2 : w(ii, z) = a(i,ii + 3) : Next
                    Else
                         With Application.WorksheetFunction
                              If .Sum(.Index(w, 1, 0)) - a(i,4) < 0 Then
                                   w(3,1) = "Ignored" : GoTo Here
                              End If
                         End With
                         For iii = 1 To UBound(w,2)
                              If w(iii, 1) >= a(i, 4) Then
                                  w(iii,1) = w(iii,1) - a(i,4)
                                  If w(iii,1) > 0 Then Exit For
                              Else
                                  a(i, 4) = a(i,4) - w(iii, 1) : w(iii,1) = 0
                              End If
                         Next
                    End If
               End If
Here:
               .item(a(i,1)) = w
          End If
     Next
     x = .keys : y = .items : Erase a
     For i = 0 To UBound(y)
          If y(i)(1,1) = "Ignored" Then .remove(x(i))
     Next
     x = .keys : y = .items
End With
With Sheets("Sheet2").Range("a3")
     For i = 0 To UBound(y)
          For ii = 1 To UBound(y(i),2)
               If y(i)(1,ii) <> 0 Then
                    .Offset(n).Value = x(i)
                    .Offset(n,1).Value = y(i)(1,ii)
                    .Offset(n,2).Value = y(i)(2,ii)
                    n = n + 1
               End If
          Next
     Next
End With
End Sub
 
Upvote 0
Thanks for the code i tried it but one error is coming When I run the code the following line showing error
Code:
    If a(i,3) = "Sell Then w(1,1) = "Ignored"
 
Upvote 0
Thanks for the code i tried it but one error is coming When I run the code the following line showing error
Code:
    If a(i,3) = "Sell Then w(1,1) = "Ignored"

Missed "
Code:
    If a(i,3) = "Sell" Then w(1,1) = "Ignored"
Previous code has been edited.
 
Upvote 0
Thanks for the reply Now error is coming on this
Code:
If Not dic.exists(a(i, 1)) Then
which is the 7th line of your code. Error message is "Run time error 424 Oject required."
 
Upvote 0
Hi,
I am not knowing more english.
I apologize.

Code:
Sub Test()
'http://www.mrexcel.com/board2/viewtopic.php?t=280040
'jindon
Dim a, i As Long, ii As Long, iii As Long, w(), x, y, z, n As Long
a = Sheets("Sheet1").Range("a1").CurrentRegion.Resize(, 5).Value
With CreateObject("Scripting.Dictionary")
     .CompareMode = vbTextCompare
     For i = 2 To UBound(a, 1)
          If Not .exists(a(i, 1)) Then
               ReDim w(1 To 2, 1 To 1)
               For ii = 1 To 2: w(ii, 1) = a(i, ii + 3): Next
               If a(i, 3) = "Sell" Then w(1, 1) = "Ignored"
               .Add a(i, 1), w
          Else
               w = .Item(a(i, 1))
               If w(1, 1) <> "Ignored" Then
                    If a(i, 3) = "Buy" Then
                         ReDim Preserve w(1 To 2, 1 To UBound(w, 2) + 1)
                         z = UBound(w, 2)
                         For ii = 1 To 2: w(ii, z) = a(i, ii + 3): Next
                    Else
                         With Application.WorksheetFunction
                              If .Sum(.Index(w, 1, 0)) - a(i, 4) < 0 Then
                                   w(1, 1) = "Ignored": GoTo Here
                              End If
                         End With
                         For iii = 1 To UBound(w, 2)
                              If w(1, iii) >= a(i, 4) Then
                                  w(1, iii) = w(1, iii) - a(i, 4)
                                  If w(1, iii) = 0 Then Exit For
                              Else
                                  a(i, 4) = a(i, 4) - w(1, iii): w(1, iii) = 0
                              End If
                         Next
                    End If
               End If
Here:
               .Item(a(i, 1)) = w
          End If
     Next
     x = .keys: y = .items: Erase a
     For i = 0 To UBound(y)
          If y(i)(1, 1) = "Ignored" Then .Remove (x(i))
     Next
     x = .keys: y = .items
End With
With Sheets("Sheet2").Range("a3")
     For i = 0 To UBound(y)
          For ii = 1 To UBound(y(i), 2)
               If y(i)(1, ii) <> 0 Then
                    .Offset(n).Value = x(i)
                    .Offset(n, 1).Value = y(i)(1, ii)
                    .Offset(n, 2).Value = y(i)(2, ii)
                    n = n + 1
               End If
          Next
     Next
End With
End Sub
 
Upvote 0
This code is made i the right director. However it is not copying the dates and the Total coumn. Can this be modified?
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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