|
- Sub test1() '。
- Dim Conn As Object, Dict As Object, strConn As String
- Dim p As String, f As String, s As String, SQL As String
- Set Dict = CreateObject("Scripting.Dictionary")
- Set Conn = CreateObject("ADODB.Connection")
- s = "Excel 12.0;HDR=no;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;HDR=no';Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=no';Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
- p = ThisWorkbook.Path & "\"
- f = Dir(p & "*.xls?")
- SQL = "SELECT * FROM [" & s & p & "[f]].[Standard$A7:D38] WHERE LEN(F3)"
- Do
- If p & f <> ThisWorkbook.FullName Then
- Dict.Add Replace(SQL, "[f]", f), vbNullString
- End If
- f = Dir
- Loop While Len(f)
- If Dict.Count Then
- SQL = Join(Dict.Keys, " UNION ALL ")
- SQL = "SELECT a.*,b.F3,b.F4 FROM [Standard$A7:B38] a LEFT JOIN (" & SQL & ") b ON a.F1=b.F1"
- Range("A7").CopyFromRecordset Conn.Execute(SQL)
- End If
- Conn.Close
- Set Conn = Nothing
- Set Dict = Nothing
- Beep
- End Sub
复制代码 |
|