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.
[TABLE="width: 528"]
<tbody>[TR]
[TD]Amount[/TD]
[TD]Price per Unit[/TD]
[TD]Total[/TD]
[TD][/TD]
[TD][/TD]
[TD]Amount[/TD]
[TD]Average per Unit[/TD]
[/TR]
[TR]
[TD="align: right"]100[/TD]
[TD] $3.00[/TD]
[TD] $300.00[/TD]
[TD][/TD]
[TD]Purchased[/TD]
[TD="align: right"]1000[/TD]
[TD] $3.25[/TD]
[/TR]
[TR]
[TD="align: right"]150[/TD]
[TD] $3.00[/TD]
[TD] $450.00[/TD]
[TD][/TD]
[TD]Sold[/TD]
[TD="align: right"]600[/TD]
[TD] $5.00[/TD]
[/TR]
[TR]
[TD="align: right"]200[/TD]
[TD] $2.00[/TD]
[TD] $400.00[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]250[/TD]
[TD] $3.00[/TD]
[TD] $750.00[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]300[/TD]
[TD] $4.50[/TD]
[TD] $1,350.00[/TD]
[TD][/TD]
[TD]Left[/TD]
[TD="align: right"]400[/TD]
[TD] $4.13[/TD]
[/TR]
[TR]
[TD="align: right"]-600[/TD]
[TD] $5.00[/TD]
[TD] $(3,000.00)[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
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.
[TABLE="width: 528"]
<tbody>[TR]
[TD]Amount[/TD]
[TD]Price per Unit[/TD]
[TD]Total[/TD]
[TD][/TD]
[TD][/TD]
[TD]Amount[/TD]
[TD]Average per Unit[/TD]
[/TR]
[TR]
[TD="align: right"]100[/TD]
[TD] $3.00[/TD]
[TD] $300.00[/TD]
[TD][/TD]
[TD]Purchased[/TD]
[TD="align: right"]1000[/TD]
[TD] $3.25[/TD]
[/TR]
[TR]
[TD="align: right"]150[/TD]
[TD] $3.00[/TD]
[TD] $450.00[/TD]
[TD][/TD]
[TD]Sold[/TD]
[TD="align: right"]600[/TD]
[TD] $5.00[/TD]
[/TR]
[TR]
[TD="align: right"]200[/TD]
[TD] $2.00[/TD]
[TD] $400.00[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]250[/TD]
[TD] $3.00[/TD]
[TD] $750.00[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]300[/TD]
[TD] $4.50[/TD]
[TD] $1,350.00[/TD]
[TD][/TD]
[TD]Left[/TD]
[TD="align: right"]400[/TD]
[TD] $4.13[/TD]
[/TR]
[TR]
[TD="align: right"]-600[/TD]
[TD] $5.00[/TD]
[TD] $(3,000.00)[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]