计算查询中的加权平均值vba access

时间:2017-03-15 08:34:32

标签: javascript sql ms-access access-vba

我正在研究一个需要计算加权平均成本的数据库 基于每单位实际成本的库存。源数据 在一个结合项目接收/返回历史的查询中, 并按项目ID和收货日期对结果进行排序。 所以,我从这个查询中得到的结果如下:


项目#RECPT_DATE QTY UNIT_COST
A1 10/15/2001 100 5.50
A1 09/02/2001 50 6.00
A1 06 / 10/2001 40 7.00
A2 12/02/2001 75 15.00
A2 07/20/2001 20 14.50

现在,我有另一张桌子告诉我现有总数
库存中每件商品的数量。

项目#TOT_QTY
A1 [加权平均成本]
A2 [加权平均成本]
那么,这可能与访问中的VBA有关吗? 任何帮助将非常感激!
THX。

Public Function SumProduct(Array1 As Variant, Array2 As Variant) As Variant

    Dim lngRowMax As Long
    Dim lngColMax As Long
    Dim i As Long
    Dim j As Long
    Dim var As Variant

    lngRowMax = IIf(UBound(Array1, 2) <= UBound(Array2, 2), UBound(Array1, 2), UBound(Array2, 2))
    lngColMax = IIf(UBound(Array1) <= UBound(Array2), UBound(Array1), UBound(Array2))
    ReDim var(0 To lngRowMax)
    For i = 0 To lngRowMax
        For j = 0 To lngColMax
            var(i) = var(i) + Array1(j, i) * Array2(j, i)
        Next j
        SumProduct = SumProduct + var(i)
    Next i

End Function

1 个答案:

答案 0 :(得分:0)

Public Function SumProduct(Array1 As Variant,Array2 As Variant)As Variant

Dim lngRowMax As Long
Dim lngColMax As Long
Dim i As Long
Dim j As Long
Dim var As Variant

lngRowMax = IIf(UBound(Array1, 2) <= UBound(Array2, 2), UBound(Array1, 2), UBound(Array2, 2))
lngColMax = IIf(UBound(Array1) <= UBound(Array2), UBound(Array1), UBound(Array2))
ReDim var(0 To lngRowMax)
For i = 0 To lngRowMax
    For j = 0 To lngColMax
        var(i) = var(i) + Array1(j, i) * Array2(j, i)
    Next j
    SumProduct = SumProduct + var(i)
Next i

结束功能