|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
ADO法速度较快,请参考:
- Sub ADO法()
- Dim Fso As Object, File As Object, cnn As Object, rs As Object, SQL$, i&, j&, n&, arr, sh(1) As Worksheet
- Application.ScreenUpdating = False
- arr = Array("[店铺-最前面添加两列$]", "[小时工-最前面加两列$]")
- Set sh(0) = Sheets("Sheet1")
- Set sh(1) = Sheets("Sheet2")
- sh(0).Cells.ClearContents
- sh(1).Cells.ClearContents
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set cnn = CreateObject("adodb.connection")
- For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
- If File.Name Like "*.xls" And File.Name <> ThisWorkbook.Name Then
- n = n + 1
- If n = 1 Then
- cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties=excel 12.0;data source=" & File
- For i = 0 To 1
- Set rs = cnn.Execute(arr(i))
- For j = 1 To rs.Fields.Count
- sh(i).Cells(1, j) = rs.Fields(j - 1).Name
- Next
- sh(i).Range("a2").CopyFromRecordset rs
- Next
- Else
- For i = 0 To 1
- SQL = "select * from [Excel 12.0;Database=" & File & ";]." & arr(i)
- sh(i).Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- Next
- End If
- End If
- Next
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Set Fso = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|