|
- Set SH1 = Sheets("Sheet1")
- Set SH2 = Sheets("Sheet2")
- Set SH3 = Sheets("Sheet2")
- SH3.Range("D2:J65536").ClearContents
-
- StrSQL = StrSQL & "SELECT C.入库物料,C.数量,D.物料号 AS 出库物料,ROUND(C.数量*D.BOM用量,4) AS 出库数量,C.父层级,D.层级 FROM ("
- StrSQL = StrSQL & "SELECT A.入库物料,B.物料号,IIF(B.层级='Parent',0,val(mid(B.层级,len(B.层级),1))) AS 父层级,A.数量 FROM ("
- StrSQL = StrSQL & "SELECT 入库物料,数量 FROM [" & SH3.Name & "$A:B]"
- StrSQL = StrSQL & ") AS A,("
- StrSQL = StrSQL & "SELECT 层级,物料号,描述,BOM用量 FROM [" & SH1.Name & "$A:D]"
- StrSQL = StrSQL & ") AS B"
- StrSQL = StrSQL & " WHERE A.入库物料=B.物料号"
- StrSQL = StrSQL & ") AS C LEFT JOIN ("
- StrSQL = StrSQL & "SELECT 层级,物料号,描述,BOM用量 FROM [" & SH1.Name & "$A:D]"
- StrSQL = StrSQL & ") AS D ON C.父层级+1=VAL(MID(D.层级,LEN(D.层级),1))"
- Str_coon = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=yes;IMEX=1';Data Source =" & ThisWorkbook.FullName '//OFFICE2007
- Dim CN, RS
- Set CN = CreateObject("Adodb.Connection") '//新建一个ADO连接
- Set RS = CreateObject("adodb.recordset")
- CN.Open Str_coon
- RS.Open StrSQL, CN, 1, 3
- For ICOL = 0 To RS.Fields.Count - 1 '循环所有查询结果的列
- SH3.Cells(1, ICOL + 4) = RS.Fields(ICOL).Name
- Next ICOL
- SH3.Range("D2").CopyFromRecordset RS
- CN.Close '//关闭ADO连接
- Set RS = Nothing
- Set CN = Nothing '//释放内存
复制代码 |
|