|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub a()
Dim cnn As New ADODB.Connection, r%, sql$, myf$
Dim RS As New ADODB.Recordset
Dim SQA$, SQB$, SQC, I%
myf = ThisWorkbook.Path & "\数据源1.xlsx"
cnn.Open "Provider=Microsoft.ACE.OleDb.12.0;Extended Properties='Excel 12.0;HDR=YES'; Data Source=" & myf
SQA = "(select * FROM [表A$A1:B] WHERE 型号 IS NOT NULL) A "
SQB = "(SELECT 型号,SUM(生产数) AS 生产数,SUM(不良数) AS 不良数 FROM [" _
& ThisWorkbook.Path & "\数据源2.xlsx].[表B$B1:E] WHERE 型号 is NOT NULL GROUP BY 型号) B"
SQC = "(SELECT 型号,SUM(销售数量) AS 销售数量 FROM [" _
& ThisWorkbook.Path & "\数据源3.xlsx].[表C$B1:C] WHERE 型号 is NOT NULL GROUP BY 型号) C "
sql = "SELECT A.型号,A.阶段,B.生产数,B.不良数,C.销售数量 FROM (" & SQA & " LEFT JOIN " _
& SQB & " ON A.型号=B.型号) LEFT JOIN " & SQC & " ON A.型号=C.型号"
Cells.Clear
RS.Open sql, cnn, 1, 1
For I = 0 To RS.Fields.Count - 1
Cells(1, I + 1) = RS.Fields(I).Name
Next
Range("a2").CopyFromRecordset RS
RS.Close
cnn.Close
Set RS = Nothing
Set cnn = Nothing
End Sub
|
|