|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
ADO法请测试:- Sub 宏1()
- Dim cnn As Object, SQL$,arr, p$, f$, n&, i&
- p = ThisWorkbook.Path & "\数据文件"
- arr = Range("A3:C5")
- n = 12
- [l:v] = ""
- For i = 1 To 3
- f = p & arr(i, 1) & ".xlsx"
- If Dir(f) <> "" Then
- If i = 1 Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='excel 12.0;hdr=no';Data Source=" & f
- SQL = "select * from [Sheet1$a" & arr(i, 2) & ":c" & arr(i, 3) & "]"
- Else
- SQL = "select * from [Excel 12.0;hdr=no;Database=" & f & "].[Sheet1$a" & arr(i, 2) & ":c" & arr(i, 3) & "]"
- End If
- Cells(1, n).CopyFromRecordset cnn.Execute(SQL)
- n = n + 4
- End If
- Next
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|