|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
ADO联合查询,请测试:- Sub 宏1()
- Dim cnn As Object, SQL$(1 To 2), Mypath$, MyFile$, n%
- Set cnn = CreateObject("adodb.connection")
- Mypath = ThisWorkbook.Path & ""
- MyFile = Dir(Mypath & "*.xlsx")
- Do While MyFile <> ""
- n = n + 1
- If n = 1 Then
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;hdr=no';Data Source=" & Mypath & MyFile
- SQL(1) = "select f1 from [Sheet1$a2:a2]"
- SQL(2) = "select f1,f3 from [Sheet1$a6:c6]"
- Else
- SQL(1) = SQL(1) & " union all select f1 from [Excel 12.0;hdr=no;Database=" & Mypath & MyFile & "].[Sheet1$a2:a2]"
- SQL(2) = SQL(2) & " union all select f1,f3 from [Excel 12.0;hdr=no;Database=" & Mypath & MyFile & "].[Sheet1$a6:c6]"
- End If
- MyFile = Dir()
- Loop
- [a1].CurrentRegion.Offset(1).ClearContents
- [a2].CopyFromRecordset cnn.Execute(SQL(1))
- [b2].CopyFromRecordset cnn.Execute(SQL(2))
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|