I would appreciate any assistance I could get with this inventory formula. Thanks!
[TABLE="width: 500"]
<tbody>[TR]
[TD]Purchased tons[/TD]
[TD]cost per ton[/TD]
[TD]tons used[/TD]
[TD]FIFO ending value[/TD]
[/TR]
[TR]
[TD]25858.23[/TD]
[TD].3518[/TD]
[TD]15858[/TD]
[TD]3518.081
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I was using the following vba code to create a FIFO formula, but it does not work with decimals in column a for some reason.
=FIFO(A2,B2,C2,TRUE)
Admittedly I am quite new to VBA and would prefer to use formulas and even helper columns but this seems to work smoother except for the decimals throw everything off...and we must have the decimals for accuracy.
Option Explicit
Function FIFO(PurchaseUnits As Range, UnitCost As Range, UnitsSold As Range, Optional blnAscending As Boolean = False) As Double
Dim Counter As Long, UnitsAccountedFor As Long
Dim varPurchased, varCost
FIFO = 0
UnitsAccountedFor = Application.Sum(UnitsSold)
varPurchased = PurchaseUnits.Value
varCost = UnitCost.Value
If blnAscending Then
Counter = LBound(varPurchased, 1)
Do Until UnitsAccountedFor = 0
If varPurchased(Counter, 1) = 0 Then
Counter = Counter + 1
Else
varPurchased(Counter, 1) = varPurchased(Counter, 1) - 1
UnitsAccountedFor = UnitsAccountedFor - 1
End If
Loop
Else
Counter = UBound(varPurchased, 1)
Do Until UnitsAccountedFor = 0
If varPurchased(Counter, 1) = 0 Then
Counter = Counter - 1
Else
varPurchased(Counter, 1) = varPurchased(Counter, 1) - 1
UnitsAccountedFor = UnitsAccountedFor - 1
End If
Loop
End If
FIFO = Application.SumProduct(varPurchased, varCost)
End Function
[TABLE="width: 500"]
<tbody>[TR]
[TD]Purchased tons[/TD]
[TD]cost per ton[/TD]
[TD]tons used[/TD]
[TD]FIFO ending value[/TD]
[/TR]
[TR]
[TD]25858.23[/TD]
[TD].3518[/TD]
[TD]15858[/TD]
[TD]3518.081
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I was using the following vba code to create a FIFO formula, but it does not work with decimals in column a for some reason.
=FIFO(A2,B2,C2,TRUE)
Admittedly I am quite new to VBA and would prefer to use formulas and even helper columns but this seems to work smoother except for the decimals throw everything off...and we must have the decimals for accuracy.
Option Explicit
Function FIFO(PurchaseUnits As Range, UnitCost As Range, UnitsSold As Range, Optional blnAscending As Boolean = False) As Double
Dim Counter As Long, UnitsAccountedFor As Long
Dim varPurchased, varCost
FIFO = 0
UnitsAccountedFor = Application.Sum(UnitsSold)
varPurchased = PurchaseUnits.Value
varCost = UnitCost.Value
If blnAscending Then
Counter = LBound(varPurchased, 1)
Do Until UnitsAccountedFor = 0
If varPurchased(Counter, 1) = 0 Then
Counter = Counter + 1
Else
varPurchased(Counter, 1) = varPurchased(Counter, 1) - 1
UnitsAccountedFor = UnitsAccountedFor - 1
End If
Loop
Else
Counter = UBound(varPurchased, 1)
Do Until UnitsAccountedFor = 0
If varPurchased(Counter, 1) = 0 Then
Counter = Counter - 1
Else
varPurchased(Counter, 1) = varPurchased(Counter, 1) - 1
UnitsAccountedFor = UnitsAccountedFor - 1
End If
Loop
End If
FIFO = Application.SumProduct(varPurchased, varCost)
End Function