|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
ADO法不用"打开",请测试:- Sub 宏1()
- Dim cnn As Object, cat As Object, tb1 As Object
- Dim SQL$, MyFile$, s$, m&, n&, t$
- Application.ScreenUpdating = False
- Set cnn = CreateObject("ADODB.Connection")
- Set cat = CreateObject("ADOX.Catalog")
- ActiveSheet.UsedRange.Offset(1).ClearContents
- 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
- Else
- t = "[Excel 8.0;Database=" & MyPath & MyFile & "]."
- End If
- cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & MyPath & MyFile
- For Each tb1 In cat.Tables
- If tb1.Type = "TABLE" Then
- s = Replace(tb1.Name, "'", "")
- If Right(s, 1) = "$" And InStr(s, "北京") > 0 Then
- m = m + 1
- If m > 49 Then
- Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- m = 1
- SQL = ""
- End If
- If Len(SQL) Then SQL = SQL & " union all "
- SQL = SQL & "select * from " & t & "[" & s & "]"
- End If
- End If
- Next
- End If
- MyFile = Dir()
- Loop
- If Len(SQL) Then Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- cnn.Close
- Set cnn = Nothing
- Set cat = Nothing
- Set tb1 = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|