excel - 如何在 Excel 中创建 fifo 函数

标签 excel fifo vba

我需要为价格计算创建一个 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/

相关文章:

r - 在 dplyr 链中转换 Excel 日期格式

python - OSError : [Errno 11] Resource temporarily unavailable. 这是什么原因造成的?

linux - 如何实现 ABA 解决方案?

c - fifo linux - write() 函数突然终止程序

Excel VBA - 我选择范围内的 findnext 无法正常工作

javascript - Office-JS 可以触发 VBA 工作簿或工作表事件过程吗?

c# - 从 .NET 传递到 VBA 的 COM 对象的对象生命周期

excel - 使用 VBA 将约束添加到求解器工具

excel - 无法找出为什么相同的 Excel VBA HTML 代码适用于某些条目而不适用于其他条目

excel - 为什么复制/粘贴与复制粘贴特殊有不同的编码?