|
请参考:
Sub ADO法()
Dim Fso As Object, File As Object, cnn As Object, rs As Object, SQL$, s$, n&, f$(), i&
Application.ScreenUpdating = False
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 "*.xlsx" And Left$(File.Name, 2) <> "~$" Then
n = n + 1
If n = 1 Then
cnn.Open "provider=microsoft.Ace.oledb.12.0;extended properties=excel 12.0;data source=" & File
SQL = "select * from [Sheet1$a6:w]"
Set rs = cnn.Execute(SQL)
ReDim f(0 To rs.Fields.Count - 1)
For i = 0 To rs.Fields.Count - 1
f(i) = rs.Fields(i).Name
Next
s = "[" & Join(f, "],[") & "]"
Range("A1").Resize(, i) = f
Range("A2").CopyFromRecordset rs
Else
SQL = "select " & s & " from [Excel 12.0;Database=" & File & ";].[Sheet1$a6:w]"
Range("A" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
End If
End If
Next
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Set File = Nothing
Set Fso = Nothing
Application.ScreenUpdating = True
End Sub |
|