我需要为价格计算创建一个 fifo 函数。
我有一个具有以下布局的表格:
Purchase_date Quantity Purchase_Price
----------------------------------------
2011-01-01 1000 10
2011-01-02 2000 11
......
Sale_date Quantity Costprice
----------------------------------------
2011-02-01 50 =fifo_costprice(...
Fifo 公式的工作原理如下:
fifo_costprice(Q_sold_to_date as float, Quantity_purchased as range
, Purchase_Prices as range) as float
如何在 Excel VBA 中执行此操作?
最佳答案
这是我想出的开始,它不做任何错误检查和日期匹配,但它有效。
Public Function fifo(SoldToDate As Double, Purchase_Q As Range, _
Purchase_price As Range) As Double
Dim RowOffset As Integer
Dim CumPurchase As Double
Dim Quantity As Range
Dim CurrentPrice As Range
CumPurchase = 0
RowOffset = -1
For Each Quantity In Purchase_Q
CumPurchase = CumPurchase + Quantity.Value
RowOffset = RowOffset + 1
If CumPurchase > SoldToDate Then Exit For
Next
'if sold > total_purchase, use the last known price.
Set CurrentPrice = Purchase_price.Cells(1, 1).offset(RowOffset, 0)
fifo = CurrentPrice.Value
End Function
关于excel - 如何在 Excel 中创建 fifo 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6762290/