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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Here's a macro.
You didn't provide column refs, so I've guessed.
Code:
Sub Bal_Unit_Cost()
Dim cel As Range, rng As Range, bal!, c%, v!
Set cel = Cells(Rows.Count, "F").End(xlUp)
Set rng = Range([A2], Cells(Rows.Count, "A").End(xlUp))
bal = cel
For c = rng.Cells.Count To 1 Step -1
    If 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
cel(1, 2) = WorksheetFunction.Round(v / cel, 2)
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Could we use a helper column (which could be hidden)?
Formula in D2 is copied down.

Excel Workbook
ABCDEFGH
1AmountPrice per UnitTotalRemaining CostAmountAverage per Unit
21003.00300.000Purchased10003.25
31503.00450.000Sold6005
42002.00400.000
52503.00750.00300
63004.501350.001350Left4004.13
7-6005.00(3000.00)0
Average
 
Upvote 0
Thank you both for your responses, I am going to go with the hidden column with formulas because I like it to auto update.
 
Upvote 0
Thank you both ..
You are very welcome. :)
Just keep an eye on the results for a while to make sure it is working as required as I have not tested it heavily.
 
Last edited:
Upvote 0
So I actually have another twist that I thought I was going to be able to take care of easily. If I am going to have 20-30 different types of items that I want to put into this same list I can't seem to figure out a way to keep them all separate. I am assuming MATCH or INDEX or something like that?


ABCD
1ItemAmountPrice per UnitTotal
2A1003.00300.00
3A1503.00450.00
4B2002.00400.00
5B2503.00750.00
6A3004.501350.00
7A-5005.00(2500.00)

<tbody>
</tbody>

<tbody></tbody>
 
Upvote 0
I'm wondering how/where the results will be laid out.
I have added a couple more rows to the sample data. Can you manually calculate the results for this data and post those results so we can see what you are expecting & how it would be presented?


Book1
ABCD
1ItemAmountPrice per UnitTotal
2A1003300
3A1503450
4B2002400
5B2503750
6A3004.51350
7A-5005-2500
8B-3504-1400
9B1005500
Current Cost
 
Upvote 0
Thank you both for your responses, I am going to go with the hidden column with formulas because I like it to auto update.
The following function will automatically update :
Code:
Function APU(cel As Range)
Dim rng As Range, bal!, c%, v!
Application.Volatile
Set rng = Range([A2], Cells(Rows.Count, "A").End(xlUp))
bal = cel
For c = rng.Cells.Count To 1 Step -1
    If 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
APU = WorksheetFunction.Round(v / cel, 2)
End Function
If the "Left" balance is in F6 (say), enter in G6 (or any other cell): =APU(F6)
 
Upvote 0
So I actually have another twist that I thought I was going to be able to take care of easily. If I am going to have 20-30 different types of items that I want to put into this same list I can't seem to figure out a way to keep them all separate. I am assuming MATCH or INDEX or something like that?


ABCD
1ItemAmountPrice per UnitTotal
2A1003.00300.00
3A1503.00450.00
4B2002.00400.00
5B2503.00750.00
6A3004.501350.00
7A-5005.00(2500.00)

<tbody>
</tbody>
My suggestion requires a separate unique list of all the Items that appear in column A - for illustration purposes I have assumed the list in column E.

Put in F2 and fill down (to get the "Left" amount for each item): =SUMIF(A:A,E2,B:B)

Use this function in column G : =APU(F2)

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, 0) 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
APU = WorksheetFunction.Round(v / cel, 2)
End Function
 
Upvote 0
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

ABCDEFGHI
1ItemAmountPrice per UnitTotalAmountAverage per Unit
2A1003.00300.00APurchased5503.82
3A1503.00450.00Sold5005.00
4B2002.00400.00Left504.50
5B2503.00750.00
6A3004.501350.00BPurchased5503.00
7A-5005.00(2500.00)Sold3504.00
8B-3504.00(1400.00)Left2004.00
9B1005.00500.00

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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