|
楼主 |
发表于 2013-5-29 20:17
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
用MSXML DOM 展开树形BOM- Sub bzk01() '树形BOM展开_XML
- Dim Arr, Brr1
- Dim T As New MSXML2.DOMDocument, R As IXMLDOMElement, Nd As IXMLDOMElement
- Dim Tep As IXMLDOMElement, Temp As IXMLDOMNodeList
- Dim A1, A2
- Dim Dret As New Dictionary, Ti, K
- Arr = Range("m1:o22").Value
- '====将Arr中数据转换成XML树
- Set R = T.createElement("root")
- T.appendChild R
- Set Nd = T.createElement(Arr(1, 2)) '物料代码
- Nd.setAttribute "level", Arr(1, 1) 'BOM层级
- Nd.setAttribute "require", Arr(1, 3) '需要数量
- Nd.setAttribute "product", 1
- R.appendChild Nd
- For i = 2 To UBound(Arr)
- Set Nd = T.createElement(Arr(i, 2))
- Nd.setAttribute "level", Arr(i, 1)
- Nd.setAttribute "require", Arr(i, 3)
- Set Temp = T.selectNodes("//*[@level=" & Arr(i, 1) - 1 & "]")
- Temp(Temp.Length - 1).appendChild Nd
- Next i
- '====对XML树做BOM展开计算
- nn = 1
- Do
- Set Temp = T.selectNodes("//*[@level=" & nn & "]")
- If Temp.Length = 0 Then Exit Do
- For Each Tep In Temp
- A1 = Val(Tep.parentNode.Attributes.Item(2).nodeValue)
- A2 = Val(Tep.Attributes.Item(1).nodeValue)
- Tep.setAttribute "product", A1 * A2
- Next Tep
- nn = nn + 1
- Loop
- Range("l25") = "BOM展开数量"
- Set Temp = T.selectNodes("//*[@*]")
- ReDim Brr1(1 To Temp.Length, 1 To 4)
- For i = 1 To UBound(Brr1)
- Brr1(i, 1) = Temp.Item(i - 1).Attributes(0).nodeValue
- Brr1(i, 2) = Temp.Item(i - 1).nodeName
- Brr1(i, 3) = Temp.Item(i - 1).Attributes(1).nodeValue
- Brr1(i, 4) = Temp.Item(i - 1).Attributes(2).nodeValue
- Next
- Range("l26").Resize(UBound(Brr1), UBound(Brr1, 2)) = Brr1
- '====物料汇总计算
- Set Temp = T.selectNodes("//*[@*]")
- For Each Tep In Temp
- Dret(Tep.nodeName) = Dret(Tep.nodeName) + Val(Tep.Attributes.Item(2).nodeValue)
- Next Tep
- K = Dret.Keys
- Ti = Dret.Items
- [p25] = "物料汇总"
- Range("p26").Resize(Dret.Count, 1) = Application.Transpose(K)
- Range("q26").Resize(Dret.Count, 1) = Application.Transpose(Ti)
- End Sub
复制代码 |
|