|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 wuque214 于 2017-3-12 22:19 编辑
采用递归的方法实现,代码如下,供交流
- Dim Arr_BOM, Arr_Demand, Arr_Stock
- Dim Dic_temp, SkuTemp, DemandDate
- Dim Arr_output
- Sub test()
- Arr_BOM = Range("H3:J34")
- Arr_Demand = Range("A2:F5")
- Arr_Stock = Range("L3:M26")
- ReDim Arr_output(1 To 4, 1 To 1)
- Arr_output(1, 1) = "Component"
- Arr_output(2, 1) = "SKU"
- Arr_output(3, 1) = "Shortage"
- Arr_output(4, 1) = "Shortage Date"
- Set Dic_temp = CreateObject("Scripting.Dictionary")
- For C = LBound(Arr_BOM, 1) To UBound(Arr_BOM, 1)
- If Not Dic_temp.exists(Arr_BOM(C, 1)) Then
- Dic_temp(Arr_BOM(C, 1)) = ""
- End If
- Next C
- For B = 2 To UBound(Arr_Demand, 2)
- For A = 2 To UBound(Arr_Demand, 1)
- SkuTemp = Arr_Demand(A, 1)
- DemandDate = Arr_Demand(1, B)
- If Arr_Demand(A, B) > 0 Then
- Call Get_data(Arr_Demand(A, 1), Deduct_Stock(SkuTemp, 1, Arr_Demand(A, B)))
- End If
- Next A
- Next B
- Workbooks.Add
- [A1].Resize(UBound(Arr_output, 2), UBound(Arr_output, 1)) = Application.Transpose(Arr_output)
- End Sub
- Private Function Get_data(PN, QTY)
- For A = 1 To UBound(Arr_BOM, 1)
- If Arr_BOM(A, 1) = PN Then
- If Dic_temp.exists(Arr_BOM(A, 2)) Then
- Get_data = Get_data(Arr_BOM(A, 2), Deduct_Stock(Arr_BOM(A, 2), Arr_BOM(A, 3), QTY))
- Else
- ReDim Preserve Arr_output(1 To UBound(Arr_output, 1), 1 To UBound(Arr_output, 2) + 1)
- Arr_output(1, UBound(Arr_output, 2)) = Arr_BOM(A, 2)
- Arr_output(2, UBound(Arr_output, 2)) = SkuTemp
- Arr_output(3, UBound(Arr_output, 2)) = Deduct_Stock(Arr_BOM(A, 2), Arr_BOM(A, 3), QTY)
- Arr_output(4, UBound(Arr_output, 2)) = DemandDate
- End If
- End If
- Next A
- End Function
- Private Function Deduct_Stock(PN, Usage, Demand_Qty)
- For A = LBound(Arr_Stock, 1) To UBound(Arr_Stock, 1)
- If Arr_Stock(A, 1) = PN Then
- If Arr_Stock(A, 2) >= Usage * Demand_Qty Then
- Arr_Stock(A, 2) = Arr_Stock(A, 2) - Usage * Demand_Qty
- Deduct_Stock = 0
- Else
- Deduct_Stock = Usage * Demand_Qty - Arr_Stock(A, 2)
- Arr_Stock(A, 2) = 0
- End If
- Exit Function
- End If
- Next A
- Deduct_Stock = Usage * Demand_Qty
- End Function
复制代码 |
|