|
请测试:- Sub Macro1()
- Dim Fso As Object, sFileType$, i&, j&, m&, n&, na$, brr(), arrf$(), mf&
- Dim cnn As Object, rs As Object, SQL$, s$
- Application.ScreenUpdating = False
- Set Fso = CreateObject("Scripting.FileSystemObject")
- sFileType = "*.xlsx"
- Call GetFiles(ThisWorkbook.Path, sFileType, Fso, arrf, mf)
- ReDim brr(1 To mf, 1 To 2)
- For i = 1 To mf
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='excel 12.0;hdr=no';Data Source=" & arrf(i)
- Set rs = cnn.OpenSchema(20) 'adSchemaTables
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- brr(i, 1) = Replace(Mid(arrf(i), InStrRev(arrf(i), "") + 1), "销售日报.xlsx", "")
- SQL = "select * from [" & s & "c6:c6]"
- brr(i, 2) = cnn.Execute(SQL)(0)
- Exit Do
- End If
- End If
- rs.MoveNext
- Loop
- Next
- [a1].CurrentRegion.Offset(1).ClearContents
- [a2].Resize(i - 1, 2) = brr
- Set Fso = Nothing
- rs.Close
- Set rs = Nothing
- cnn.Close
- Set cnn = 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) = File
- 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
复制代码 |
|