|
本帖最后由 opiona 于 2015-9-25 17:10 编辑
代码适用于三级工序:成品、半成品、零件
需求中可以有:成品、半成品、零件,
代码的区别是:开头分别为:03/02/01,
也可以是其他形式,在代码中相应修改
原理:
1、将需求的成品03根据BOM分解为02和01
2、将需求中直接是:半成品02
3、将步骤1和2中的半成品02合并,再根据Bom分解为:零件01
4、需求中直接就是:零件01
5、将步骤1中03分解出来的01、步骤3中02分解出来的01、步骤4中直接是01 全部汇总求和
部分代码如下:
- '*********************************
- '******* 北极狐工作室出品 ******
- '******* QQ:14885553 ******
- '*********************************
- Sub Opiona()
- 'On Error Resume Next '// 发生错误,自动执行下一句,就是忽略错误
- Application.ScreenUpdating = False '//关闭屏幕刷新
- Application.DisplayAlerts = False '//关闭系统提示
- t = Timer '//开始时间
- Set SH0 = Sheets("BOM示例") '//数据源
- Set SH1 = Sheets("需求") '//需求
- Set SH2 = Sheets("所有原料数量")
- SH2.Range("A2:Z65536").ClearContents
-
- Str_coon = "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & ThisWorkbook.FullName '//OFFICE2003
- StrSQL = ""
-
- Rem 需求表中03转化为02和01
- StrSQL1 = StrSQL1 & "SELECT 子物料编码,子单位,SUM(单耗AA) AS 单耗AC,SUM(单耗BA) AS 单耗AD FROM ("
- StrSQL1 = StrSQL1 & "SELECT A.物料编码,B.子物料编码,B.子单位,A.计划AA*B.单耗 AS 单耗AA,A.计划BA*B.单耗 AS 单耗BA FROM ("
- StrSQL1 = StrSQL1 & "SELECT 物料编码,[" & SH1.Cells(1, 4) & "] AS 计划AA,[" & SH1.Cells(1, 5) & "] AS 计划BA FROM [" & SH1.Name & "$] WHERE MID(物料编码,1,2)='03'"
- StrSQL1 = StrSQL1 & ") AS A LEFT JOIN ("
- StrSQL1 = StrSQL1 & "SELECT 物料编码,子物料编码,子单位,单耗 FROM [" & SH0.Name & "$]"
- StrSQL1 = StrSQL1 & ") AS B ON A.物料编码=B.物料编码"
- StrSQL1 = StrSQL1 & ") GROUP BY 子物料编码,子单位"
-
-
- Rem 全部01汇总:
- StrSQL = StrSQL & "SELECT 子物料编码 AS 物料编码,子单位 AS 单位,SUM(单耗BC) AS 计划AA,SUM(单耗BD) AS 计划BB FROM ("
-
- Rem 02转换为01:
- StrSQL = StrSQL & "SELECT D.子物料编码,D.子单位,C.单耗AC*D.单耗 AS 单耗BC,C.单耗AD*D.单耗 AS 单耗BD FROM ("
-
- Rem 03提取后结果中的02
- StrSQL = StrSQL & "SELECT 子物料编码 AS 物料编码,单耗AC,单耗AD FROM (" & StrSQL1 & ") WHERE MID(子物料编码,1,2)='02'"
- StrSQL = StrSQL & " UNION ALL "
- Rem 需求表中直接是02的
- StrSQL = StrSQL & "SELECT 物料编码,[" & SH1.Cells(1, 4) & "] AS [单耗AC],[" & SH1.Cells(1, 5) & "] AS [单耗AD] FROM [" & SH1.Name & "$] WHERE MID(物料编码,1,2)='02'"
-
- StrSQL = StrSQL & ") AS C LEFT JOIN ("
- StrSQL = StrSQL & "SELECT 物料编码,子物料编码,子单位,单耗 FROM [" & SH0.Name & "$] WHERE MID(物料编码,1,2)='02'"
- StrSQL = StrSQL & ") AS D ON C.物料编码=D.物料编码"
-
- Rem 03提取后结果中的01
- StrSQL = StrSQL & " UNION ALL "
- StrSQL = StrSQL & "SELECT 子物料编码,子单位,单耗AC AS 单耗BC,单耗AD AS 单耗BD FROM (" & StrSQL1 & ") WHERE MID(子物料编码,1,2)='01'"
-
- Rem 需求表中直接是01的
- StrSQL = StrSQL & " UNION ALL "
- StrSQL = StrSQL & "SELECT 物料编码 AS 子物料编码,单位 AS 子单位,[" & SH1.Cells(1, 4) & "] AS 单耗BC,[" & SH1.Cells(1, 5) & "] AS 单耗BD FROM [" & SH1.Name & "$] WHERE MID(物料编码,1,2)='01'"
-
- StrSQL = StrSQL & ") GROUP BY 子物料编码,子单位 ORDER BY 子物料编码"
-
- SQLARR = GET_SQL_To_Arr(StrSQL, Str_coon, False)
- SH2.Range("B2").Resize(UBound(SQLARR, 1) + 1, UBound(SQLARR, 2) + 1) = SQLARR
-
- Rem 序号
- For I = 2 To SH2.Range("B65536").End(3).Row
- SH2.Cells(I, 1) = I - 1
- Next I
-
- Application.ScreenUpdating = True '//恢复屏幕刷新
- Application.DisplayAlerts = True '//恢复系统提示
- MsgBox "一共用时:" & Format(Timer - t, "#0.0000") & " 秒", , "北极狐提示!!" '//提示所用时间
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|