VBA help Average Cost calculation for each item

marshamc

New Member
Joined
Sep 24, 2014
Messages
1
Hi,

I need some help to modify this code I got from this link:

http://www.mrexcel.com/forum/excel-questions/167756-inventory-fifo-lifo-average-cost.html

I only needed the average cost section, so I extracted that only. However, I want to modify the code such that I could calculate the average cost based on the inventory item in order of the date.


Quantity Cost
ProductDATE UNIT PRICE INOUT BALANCEUnit Price DEBIT CREDIT BALANCE
101-Sep-05 10.00 100100 1,000.00 - 1,000.00
112-Sep-05 20.00300400 6,000.00 - 7,000.00
212-Sep-05 25.00170570 4,250.00 - 11,250.00
112-Sep-0550520 19.74 - 986.84 10,263.16
223-Sep-0570450 - - 10,263.16
227-Sep-05 30.007001150 21,000.00 - 31,263.16
329-Sep-05 15.004501600 6,750.00 - 38,013.16
329-Sep-052501350 - - 38,013.16
102-Oct-05 25.003201670 8,000.00 - 46,013.16
203-Oct-055001170 - - 46,013.16
304-Oct-05101160 - - 46,013.16
2 5.004001560 2,000.00 - 48,013.16
1201540 15.64 - 312.77 47,700.39
3 50.001001640 5,000.00 - 52,700.39
1501590 19.50 - 974.97 51,725.41
21590 - - 51,725.41

<tbody>
</tbody>


The codes are below:

On the sheet:

Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Count > 1 Then Exit Sub
If Not IsNumeric(.Value) Then Exit Sub
If Not Intersect(.Cells(1, 1), Range("e:g")) Is Nothing And _
.Row > 6 Then AVR_COST
End With
End Sub


Module:

Sub AVR_COST()
Dim a, i As Long, Bal As Double, Debit As Double
Dim AVcost As Double

With Application
.Calculation = xlCalculationManual
.EnableEvents = False
End With

With Sheets("AVR COST")
a = .Range("e7", .Cells(.Rows.Count, "g").End(xlUp)).Resize(, 3).Value
.Range("i7", .Cells(.Rows.Count, "i").End(xlUp)).ClearContents
ReDim Preserve a(1 To UBound(a, 1), 1 To 4)

For i = LBound(a, 1) To UBound(a, 1)
If a(i, 2) > 0 Then
Bal = Bal + a(i, 2)
Debit = Debit + a(i, 1) * a(i, 2)
AVcost = Debit / Bal
ElseIf a(i, 3) > 0 Then
a(i, 4) = AVcost
Debit = Debit - a(i, 3) * AVcost
Bal = Bal - a(i, 3)
End If

Next
.Range("i7").Resize(UBound(a, 1)) = Application.Index(a, 0, 4)
Erase a

End With

With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With

End Sub


Thanks a million.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,956
Messages
5,856,507
Members
431,819
Latest member
hellosunshine

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
Top