| 
 | 
9#
 
 
 楼主 |
发表于 2012-12-12 12:44:58
|
只看该作者
 
 
 
Sub Filelist() 
    Dim Fso As Object, sFileType$, strPath$, i&, j&, lc&, arrf$(), mf&, arr, brr() 
    With Application.FileDialog(msoFileDialogFolderPicker) 
        .AllowMultiSelect = True 
        If .Show Then 
            strPath = .SelectedItems(1) 
        Else 
            Exit Sub 
        End If 
    End With 
    Application.ScreenUpdating = False 
    Set Fso = CreateObject("Scripting.FileSystemObject") 
    If Application.Version <= 11 Then sFileType = "*.xls" Else sFileType = "*.xls*" 
    Call searchFile(strPath, sFileType, Fso, arrf, mf) 
    If mf Then 
        ReDim brr(1 To mf, -1 To 254) 
        For i = 1 To mf 
            brr(i, -1) = arrf(1, i) 
            brr(i, 0) = arrf(2, i) 
            With GetObject(arrf(1, i) & "\" & arrf(2, i)) 
                With .Sheets(1) 
                    arr = .Range("a1", .Cells(1, .Columns.Count).End(1)) 
                End With 
                .Close False 
            End With 
            For j = 1 To UBound(arr, 2) 
                brr(i, j) = arr(1, j) 
            Next 
            If j > lc Then lc = j 
        Next 
    End If 
    Cells.Clear 
    [a1].Resize(mf, lc + 1) = brr 
    Set Fso = Nothing 
    Application.ScreenUpdating = True 
End Sub 
 
Private Sub searchFile(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 2, 1 To mf) 
                arrf(1, mf) = sPath 
                arrf(2, mf) = File.Name 
            End If 
        End If 
    Next 
    If Folder.SubFolders.Count > 0 Then 
        For Each SubFolder In Folder.SubFolders 
            Call searchFile(SubFolder.Path, sFileType, Fso, arrf, mf) 
        Next 
    End If 
    Set Folder = Nothing 
    Set File = Nothing 
    Set SubFolder = Nothing 
End Sub |   
 
 
 
 |