|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
beleqing 发表于 2013-12-29 23:24
110 10 34
111 23 12
112 32 45
修改如下:- Sub 宏1()
- Dim cnn As Object, SQL$, Mypath$, MyFile$, m&, n&, t$
- Dim Fso As Object, arrf$(), mf&, sFileType$, j&
- Application.ScreenUpdating = False
- sFileType = "*.xlsx"
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Call GetFiles(ThisWorkbook.Path, sFileType, Fso, arrf, mf)
- ActiveSheet.UsedRange.Offset(1).ClearContents
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & arrf(1)
-
- For j = 1 To mf
- 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 [Excel 12.0;Database=" & arrf(j) & "].[Sheet1$]"
- 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)
- If sPath <> ThisWorkbook.Path Then
- For Each File In Folder.Files
- If File.Name Like sFileType Then
- mf = mf + 1
- ReDim Preserve arrf(1 To mf)
- arrf(mf) = sPath & "" & File.Name
- End If
- Next
- End If
- 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
复制代码 |
|