Calculate most current price per unit from a list

zmilot

New Member
Joined
Feb 23, 2016
Messages
5
I am trying to figure out a way to get the most current average cost of however many units are still in stock.

An example is shown below of the type of list that I will keep adding to whenever I make a purchase or a sale. The part that I am having trouble calculating is the average cost of what is still in stock (in red). I can calculate it manually but would obviously prefer for it to be automated. I want the formula or code to take the 400 units that are in stock and start from the bottom of the list and take the 300 from the most recent purchase that cost $4.50 each and 100 from the next most recent purchase that cost $3.00 and calculate that the most recently purchased 400 units cost $4.13.

Any help would be appreciated.

AmountPrice per UnitTotalAmountAverage per Unit
100 $3.00 $300.00Purchased1000 $3.25
150 $3.00 $450.00Sold600 $5.00
200 $2.00 $400.00
250 $3.00 $750.00
300 $4.50 $1,350.00Left400 $4.13
-600 $5.00 $(3,000.00)

<tbody>
</tbody>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
It doesn't have to be exactly formatted like this but this was my general idea. So same as before I want A to calculate the most recently purchased 50 items and B to calculate the most recent 200 items. I am pretty good at deciphering what a formula does when I see it so I should be able to add additional items and reorder things later on. I just cant figure out a way to keep the different items separate.

Thanks again
If you have the layout set up like that, the formulas for all except the average for the remaining units and want to continue with a non-macro solution ..

E2 copied down
I4 copied to I8, I12, etc

Excel Workbook
ABCDEFGHI
1ItemAmountPrice per UnitTotalRemaining CostAmountAverage per Unit
2A10033000APurchased5503.82
3A15034500Sold5005
4B20024000Left504.5
5B2503750300
6A3004.51350225BPurchased5503
7A-5005-25000Sold3504
8B-3504-14000Left2004
9B1005500500
Average Cost
 
Upvote 0
Based on your format in post#10, the formulas :

H2 : =SUMIFS(B:B,A:A,F2,B:B,">0")
I2 : =SUMIFS(D:D,B:B,">0",A:A,F2)/H2
H3 : =-SUMIFS(B:B,A:A,F2,B:B,"<0")
H4 : =H2-H3
I4 : =APU(H4)

Code:
Function APU(cel As Range)
Dim rng As Range, bal!, c%, v!
Application.Volatile
Set rng = Range([B2], Cells(Rows.Count, "B").End(xlUp))
bal = cel
For c = rng.Cells.Count To 1 Step -1
    If rng(c)(1, 0) = cel(-1, -1) And rng(c) > 0 Then
        If bal > rng(c) Then
            v = v + rng(c) * rng(c)(1, 2)
            bal = bal - rng(c)
        Else
            v = v + bal * rng(c)(1, 2)
            Exit For
        End If
    End If
Next
If v <> 0 Then APU = WorksheetFunction.Round(v / cel, 2)
End Function
 
Upvote 0
You might also want the Cost of Goods sold per unit :

=(SUMIFS(D:D,B:B,">0",A:A,F2)-(H4*I4))/H3
 
Upvote 0
Thank you both for your help and responses. I tried both of your solutions and they seem to work great. I am probably going to stick with using the strictly formula based answer simply because I can expand and troubleshoot them easier.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

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