|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
请测试
Sub 宏1()
Dim cnn As Object, SQL$, Mypath$, MyFile$, n&
Set cnn = CreateObject("Adodb.Connection")
Mypath = ThisWorkbook.Path & "\"
MyFile = Dir(Mypath & "*.xlsx")
Do While MyFile <> ""
If MyFile <> 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=" & Mypath & MyFile
SQL = "select * from [Sheet1$a2:c1048576] where 城市='临沂分公司'"
Else
SQL = SQL & " union all select * from [Excel 12.0;Database=" & Mypath & MyFile & "].[Sheet1$a2:c1048576] where 城市='临沂分公司'"
End If
End If
MyFile = Dir()
Loop
If n = 0 Then
MsgBox "没有发现可以复制的文件!", vbInformation, "提示"
Exit Sub
End If
ActiveSheet.UsedRange.Offset(2).ClearContents
[a3].CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
End Sub
|
|