计算总数量

时间:2015-10-06 15:59:29

标签: excel vba

我有一种情况,即在Excel中导出的物料清单会进入' Level''项目'和'数量'。为了计算BoM中项目的总数量,有必要将数量乘以父级别的数量。我在下面手动显示了这一点,但由于实际数据集的大小,我想知道是否有可用的方法使用VBA来计算总数量值?

import string
from collections import namedtuple
from collections import defaultdict
from collections import OrderedDict
import time

trans = defaultdict(dict)
...
matrix_col = OrderedDict(sorted(matrix_col.items(), key=lambda t: t[0]))
trans_mat = []
counter = 0

for u1, v1 in matrix_col.items():
    print counter, time.ctime()
    for u2, v2 in matrix_col.items():
        flag = True
        for w1 in trans.keys():
            for w2, c in trans[u1].items():
                if u1 == str(w1) and u2 == str(w2):
                    trans_mat.append([c])    
                    flag = False
        if flag:
            trans_mat.append([0])

trans_mat = np.asarray(trans_mat)
trans_mat = np.reshape(trans_mat, (11000, 11000))

1 个答案:

答案 0 :(得分:0)

这是一个要使用的UDF。

将其粘贴到您正在使用的工作簿中的模块中。

Function qtyfind(level As Range) As Double
    Dim i&, temp&, qtyClm&, tQtyClm&

    'Change the Column name to match if different
    qtyClm = WorksheetFunction.Match("QTY", Range("1:1"))
    tQtyClm = WorksheetFunction.Match("TOTAL QTY", Range("1:1"))

    If level = 1 Then
        temp = Cells(level.Row, qtyClm)
    Else
        For i = level.Row To 2 Step -1 'loops bottom up

            If Cells(i, level.Column) = (level.value - 1) Then
                temp = Cells(i, tQtyClm) * Cells(level.Row, qtyClm)
                Exit For
            End If
        Next
    End If

    qtyfind = temp
End Function

然后在第一个单元格中调用它并复制下来:

enter image description here