|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 据想象而写() '
Dim strFile(1) As String
strFile(0) = ThisWorkbook.Path & "\工作簿1.xlsx"
strFile(1) = ThisWorkbook.Path & "\工作簿2.xlsx"
If Dir(strFile(0)) = "" Or Dir(strFile(1)) = "" Then MsgBox "文件不存在!", 64: Exit Sub
Dim Conn As New ADODB.Connection, rs As New ADODB.Recordset
Dim strConn As String, SQL As String, s As String, i As Integer
s = "Excel 12.0;HDR=Yes;IMEX=1;Database="
If Application.Version < 12 Then
s = Replace(s, "12.0", "8.0")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
Else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
End If
Conn.Open strConn & strFile(0)
'SELECT [订单$].订单号, [订单$].品名, [订单$].数量,[生产$].车台,[客户要求$].包装要求 FROM ([订单$] LEFT JOIN [生产$] ON [订单$].品名 = [生产$].品名) LEFT JOIN [客户要求$] ON [订单$].客户 = [客户要求$].客户
SQL = "SELECT a.订单号,a.品名,a.数量,b.车台,c.包装要求 FROM ([订单$] a LEFT JOIN [" & s & strFile(1) & "].[生产$] b ON a.品名=b.品名) LEFT JOIN [客户要求$] c ON a.客户=c.客户"
rs.Open SQL, Conn, adOpenKeyset, adLockOptimistic
For i = 0 To rs.Fields.Count - 1
Range("A2").Offset(0, i) = rs.Fields(i).Name
Next
Range("A3").CopyFromRecordset rs
rs.Close
Set rs = Nothing
Conn.Close
Set Conn = Nothing
Beep
End Sub |
评分
-
2
查看全部评分
-
|