|
ADO法请测试:- Dim arrf(), mf&
- Sub Macro1()
- Dim Mypath$, Fso As Object, i&, m&, brr(1 To 60000, 1 To 3)
- Dim cnn As Object, rs As Object, rst As Object
- With Application.FileDialog(msoFileDialogFolderPicker)
- If .Show = False Then Exit Sub
- Mypath = .SelectedItems(1) ' & ""
- End With
- Application.ScreenUpdating = False
- Set Fso = CreateObject("Scripting.FileSystemObject")
- sFileType = "*.xls"
- Call GetFiles(Mypath, sFileType, Fso)
- For i = 1 To mf
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;hdr=no';Data Source=" & arrf(i)
- Set rst = cnn.OpenSchema(20) 'adSchemaTables
- Do Until rst.EOF
- If rst.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rst("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- Set rs = cnn.Execute("[" & s & "a1:a1]")
- If rs.Fields(0).Value <> "" Then
- m = m + 1
- brr(m, 1) = m
- brr(m, 2) = rs.Fields(0).Value
- brr(m, 3) = cnn.Execute("[" & s & "a3:a3]").Fields(0).Value
- End If
- End If
- End If
- rst.MoveNext
- Loop
- Next
- Range("A5:F65536").ClearContents
- [a5].Resize(m, 3) = brr
- rs.Close
- rst.Close
- cnn.Close
- Set rs = Nothing
- Set rst = Nothing
- Set cnn = Nothing
- mf = 0
- Erase arrf
- Set Fso = Nothing
- Application.ScreenUpdating = True
- End Sub
- Private Sub GetFiles(ByVal sPath$, ByVal sFileType$, Fso As Object)
- 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
- For Each SubFolder In Folder.SubFolders
- Call GetFiles(SubFolder.Path, sFileType, Fso)
- Next
- Set Folder = Nothing
- Set File = Nothing
- Set SubFolder = Nothing
- End Sub
复制代码 |
|