|
一个BOM表计算问题,效率不是很搞,但应该能满足贴主需求
- Private vData As Variant, nRow As Long
- Private dicItem As Object, dicParent As Object
- Private vLevel As Variant
- Sub BOM统计()
- Dim nVal As Double, vItem As Variant
-
- Application.ScreenUpdating = False
- Set dicItem = CreateObject("Scripting.Dictionary") 'BOM表项目值
- Set dicParent = CreateObject("Scripting.Dictionary") 'BOM表子项对应的父项
- With Sheet1
- nRow = .Cells(.Rows.Count, 1).Row
- vData = .[A1:B1].Resize(nRow, 2).Value
- For nRow = 1 To UBound(vData)
- If vData(nRow, 1) <> "" Then
- vLevel = Split(vData(nRow, 1), ".")
- If UBound(vData, 2) - 2 < UBound(vLevel) + 1 Then ReDim Preserve vData(1 To UBound(vData), 1 To 2 + UBound(vLevel) + 1)
- SetDic
- End If
- Next
-
- For Each vItem In dicItem.Keys
- nVal = dicItem(vItem)
- Do While dicParent.Exists(vItem)
- vItem = dicParent(vItem)
- dicItem(vItem) = dicItem(vItem) + nVal
- Loop
- Next
- For nRow = 1 To UBound(vData)
- vData(nRow, 2) = dicItem(Trim(vData(nRow, 1)))
- Next
- .[A1:B1].Resize(UBound(vData), 2) = vData
- End With
- Application.ScreenUpdating = True
- End Sub
- Private Function SetDic(Optional ByVal Level As Long = 1, Optional ByVal Parent As Variant = Empty)
- Dim vKey As Variant, vSon As Variant
-
- vKey = vLevel(Level - 1)
- vData(nRow, 2 + Level) = vKey
-
- If Parent <> "" Then vSon = Parent & "."
- vSon = vSon & vKey
- If Parent <> "" Then dicParent(vSon) = Parent
-
- If Level = UBound(vLevel) + 1 Then
- dicItem(vSon) = vData(nRow, 2)
- Else
- If dicItem.Exists(vSon) Then dicItem.Remove vSon
- SetDic Level:=Level + 1, Parent:=vSon
- End If
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|