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
 
Code:
Sub Test()
'http://www.mrexcel.com/board2/viewtopic.php?t=280040
'jindon
Dim a, i As Long, ii As Long, iii As Long, iv As Long, w(), x, y, z, n As Long
a = Sheets("Sheet1").Range("a1").CurrentRegion.Resize(, 6).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 4, 1 To 1)
               For ii = 1 To 3: w(ii, 1) = a(i, ii + 3): Next
               w(4, 1) = a(i, 2)
               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 4, 1 To UBound(w, 2) + 1)
                         z = UBound(w, 2)
                         For ii = 1 To 3: w(ii, z) = a(i, ii + 3): Next
                         w(4, z) = a(i, 2)
                    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
                                  esk = w(1, iii)
                                  w(1, iii) = w(1, iii) - a(i, 4)
'                                  MsgBox esk & vbCr & w(1, iii) & vbCr & w(3, iii) & vbCr & w(3, iii) / esk * w(1, iii)
                                  w(3, iii) = w(3, iii) / esk * w(1, iii)
                                  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")
    .CurrentRegion.Offset(1).ClearContents
     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)(4, ii)
                    .Offset(n, 2).Value = "Buy"
                    .Offset(n, 3).Value = y(i)(1, ii)
                    .Offset(n, 4).Value = y(i)(2, ii)
                    .Offset(n, 5).Value = y(i)(3, ii) '.Offset(n, 3).Value * .Offset(n, 4).Value
                    n = n + 1
               End If
          Next
     Next
End With
End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
THanks you This is fully working.
Just one more thing

1. Can subtotoal function be added to the same ie the code will show the subtotal at each change in items and then bold and shade the same with back ground color?

2. At the subtotal row, average price should also come as shown in the HTML image which will be derived by dividing the Total amount by Total Qtn.
FIFO.xls
ABCDEF
1
2Name of the StockDateQtn.PriceAmount
3A01/09/2005Buy100828275.97
4A17/10/2005Buy10073.87448.38
5A Total20078.6215724.35
6C17/01/2005Buy20018.353703.08
7C03/03/2006Buy9725.552508.38
8C26/04/2006Buy10324.052507.69
9C05/12/2006Buy10030.83111.91
10C08/03/2007Buy10032.953327.18
11C Total60025.2615158.24
12D15/01/2007Buy100128.512974.24
13D Total100129.7412974.24
14E08/03/2007Buy10064.656527.36
15E Total10065.276527.36
16HH31/05/2007Buy10062.56310.48
17HH Total10063.106310.48
18ID07/12/2004Buy1050.85513.0843
19ID Total1051.31513.0843
Sheet2
 
Upvote 0
Code:
Sub Formatting()
Set wf = WorksheetFunction
Sheets("Sheet2").Select
[a3:f65536].Font.Bold = False
[a3:f65536].Interior.ColorIndex = xlNone
st = 4
strt:
For x = st To Cells(Rows.Count, 1).End(3).Row + 1
    If Cells(x, 1) <> Cells(x - 1, 1) Then
        Rows(x).Insert
            With Range(Cells(x, "a"), Cells(x, "f"))
                .Interior.ColorIndex = 15
                .Font.Bold = True
            End With
            Cells(x, 1) = Cells(x, 1).Offset(-1) & " Total"
            Cells(x, 4) = wf.Sum(Range(Cells(st - 1, 4), Cells(x - 1, 4)))
            Cells(x, 6) = Round(wf.Sum(Range(Cells(st - 1, 6), Cells(x - 1, 6))), 2)
            Cells(x, 5) = Round(Cells(x, 6) / Cells(x, 4), 2)
        st = x + 2
        GoTo strt
    End If
Next x
Set wf = Nothing
End Sub
 
Upvote 0
Thanks guys,
try this one
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(, 6).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 6, 1 To 1)
               For ii = 1 To 6: w(ii, 1) = a(i, ii): 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 6, 1 To UBound(w, 2) + 1)
                         z = UBound(w, 2)
                         For ii = 1 To 6: w(ii, z) = a(i, ii): 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(4, iii) >= a(i, 4) Then
                                  w(4, iii) = w(4, iii) - a(i, 4)
                                  w(6, iii) = _
                                       WorksheetFunction.Round(w(4, iii) * w(5, iii), 2)
                                  If w(4, iii) = 0 Then Exit For
                              Else
                                  a(i, 4) = a(i, 4) - w(4, iii): w(4, 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
Set r = Sheets("sheet2").Range("a3")
With Sheets("Sheet2").Range("a3")
     .Resize(Rows.Count - 3).EntireRow.Clear
     myMin = 1
     For i = 0 To UBound(y)
          For ii = 1 To UBound(y(i), 2)
               If y(i)(1, ii) <> 0 Then
                    For iii = 1 To 6
                        .Offset(n, iii - 1).Value = y(i)(iii, ii)
                    Next
                    n = n + 1
                    myMax = WorksheetFunction.Max(myMax, n)
               End If
          Next
          .Offset(n).Value = x(i) & " Total"
          .Offset(n, 3).Resize(, 3).Formula = _
               Array("=subtotal(9,d" & myMin + 2 & ":d" & myMax + 2 & ")", _
                        "=round(average(e" & myMin + 2 & ":e" & myMax + 2 & "),2)", _
                        "=subtotal(9,f" & myMin + 2 & ":f" & myMax + 2 & ")")
          With .Offset(n).Resize(, 6)
               .Interior.ColorIndex = 15
               .Font.Bold = True
          End With
          myMin = myMax + 2
          n = n + 1
     Next
End With
End Sub
 
Upvote 0
thank guys, but I think I have to modify the codes to suit my range . WOuld someone from you please include comments with respect to where the ranges say for rows or columns I have to change or simply modify the code to cover entire sheet 1 as the data changes day by day.
 
Upvote 0
thank guys, but I think I have to modify the codes to suit my range . WOuld someone from you please include comments with respect to where the ranges say for rows or columns I have to change or simply modify the code to cover entire sheet 1 as the data changes day by day.
You need to tell me exactly how the sheet laid out.
 
Upvote 0
Thnaks The sheet is laid out exactly in the same manner as shown in my earlier posts but data are more ie column are same but rows are more
 
Upvote 0
The the code should work as it is unless you have empty row/column within the data range.
 
Upvote 0
yeah the code is working Sorry I was wrong Now the result is coming correct. Thanks for the effort and help that you and others have provided.
 
Upvote 0

Forum statistics

Threads
1,215,362
Messages
6,124,502
Members
449,166
Latest member
hokjock

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