|
从Excel2007后
Jet.OLEDB.4.0
改为
.Ace.OLEDB.12.0
Excel 8.0
改为
Excel 12.0
Sub Macro1()
Dim Fso As Object, File As Object, cnn As Object, SQL$, m&
Application.ScreenUpdating = False
Set Fso = CreateObject("Scripting.FileSystemObject")
ActiveSheet.UsedRange.Offset(2, 1).ClearContents
For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
If File.Name Like "*.xlsx" Then
m = m + 1
If m = 1 Then
Set cnn = CreateObject("adodb.connection")
cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;hdr=no';Data Source=" & File
SQL = "select b.f2 from [Excel 12.0;hdr=no;Database=" & ThisWorkbook.FullName & ";].[" & ActiveSheet.Name & "$a4:a" & Range("a" & Rows.Count).End(xlUp).Row & "] a left join [清算表$a3:b] b on a.f1=b.f1"
Else
SQL = "select b.f2 from [Excel 12.0;hdr=no;Database=" & ThisWorkbook.FullName & ";].[" & ActiveSheet.Name & "$a4:a" & Range("a" & Rows.Count).End(xlUp).Row & "] a left join [Excel 12.0;hdr=no;Database=" & File & ";].[清算表$a3:b] b on a.f1=b.f1"
End If
Cells(4, m + 1).CopyFromRecordset cnn.Execute(SQL)
Cells(3, m + 1) = Replace(File.Name, ".xls", "")
End If
Next
Set Fso = Nothing
cnn.Close
Set cnn = Nothing
Application.ScreenUpdating = True
End Sub
|
|