|
请测试:- Sub Macro1()
- Dim cnn As Object, SQL$, m&, n&, t$, Fso As Object, arrf$(), mf&
- Application.ScreenUpdating = False
- Set Fso = CreateObject("Scripting.FileSystemObject")
- sFileType = "*.xlsx"
- Call GetFiles(ThisWorkbook.Path, sFileType, Fso, arrf, mf)
- ActiveSheet.UsedRange.Offset(1).ClearContents
- Set cnn = CreateObject("ADODB.Connection")
- For n = 1 To mf
- If n = 1 Then
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & arrf(n)
- Else
- t = "[Excel 12.0;Database=" & arrf(n) & "]."
- End If
- 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 & "[Sheet1$] where 地区 is not null"
- Next
- If Len(SQL) Then Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- cnn.Close
- Set cnn = Nothing
- Set Fso = Nothing
- Application.ScreenUpdating = True
- End Sub
- Private Sub GetFiles(ByVal sPath$, ByVal sFileType$, ByRef Fso As Object, ByRef arrf$(), ByRef mf&)
- Dim Folder As Object
- Dim SubFolder As Object
- Dim File As Object
- Set Folder = Fso.GetFolder(sPath)
-
- For Each File In Folder.Files
- If File.Name Like sFileType Then
- If File.Name <> ThisWorkbook.Name Then
- mf = mf + 1
- ReDim Preserve arrf(1 To mf)
- arrf(mf) = sPath & "" & File.Name
- End If
- End If
- Next
- If Folder.SubFolders.Count > 0 Then
- For Each SubFolder In Folder.SubFolders
- Call GetFiles(SubFolder.Path, sFileType, Fso, arrf, mf)
- Next
- End If
- Set Folder = Nothing
- Set File = Nothing
- Set SubFolder = Nothing
- End Sub
复制代码 |
|