How to loop through all SubFolders and get contents of Row 1 of each Excel file?

前端 未结 2 2023
暖寄归人
暖寄归人 2021-01-27 09:30

I am trying to figure out a way to open all Excel files in all SubFolders, and get the all values from all cells in Row 1 and all formats for all these cells. I think my code b

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-27 10:09

    It can do like this also.

    Sub GetFileFromFolder()
        Dim n           As Long
        Dim fd As FileDialog
        Dim strFolder As String
        Dim colResult As Collection
        Dim i As Long, k As Long
        Dim vSplit
        Dim strFn As String
        Dim vR() As String
        Dim p As String
        Dim Wb As Workbook
        Dim sht As Worksheet, Ws As Worksheet
        Dim rng As Range, rngDB As Range
    
    
        Set sht = ThisWorkbook.Worksheets("Sheet1")
    
            p = Application.PathSeparator
            Set fd = Application.FileDialog(msoFileDialogFolderPicker)
            With fd
                .Show
                .InitialView = msoFileDialogViewList
                .Title = "Select Folder"
                .AllowMultiSelect = False
                If .SelectedItems.Count = 0 Then
                Else
                    strFolder = .SelectedItems(1)
                    Set colResult = SearchFolder(strFolder)
    
                    i = colResult.Count
                    For k = 1 To i
                        If colResult(k) Like "*.xls*" Then
                            n = n + 1
                            ReDim Preserve vR(1 To 5, 1 To n)
                            Set Wb = Workbooks.Open(colResult(k))
                            Set Ws = Wb.Worksheets(1)
    
                            lngColCount = Ws.UsedRange.Columns.Count
    
                            vSplit = Split(colResult(k), p)
                            strFn = vSplit(UBound(vSplit))
                            vR(1, n) = strFn
                            vR(2, n) = Left(colResult(k), Len(colResult(k)) - Len(strFn))
                            vR(3, n) = colResult(k)
                            vR(4, n) = Ws.Cells(1, lngColCount).Value
                            vR(5, n) = Ws.Cells(1, lngColCount).NumberFormat
                            Wb.Close (0)
                        End If
                    Next k
                    With sht
                        .UsedRange.Clear
                        .Range("A1").Value = "Name"
                        .Range("B1").Value = "Path"
                        .Range("a2").Resize(n, 5) = WorksheetFunction.Transpose(vR)
                        Set rngDB = .Range("c2").Resize(n)
                        For Each rng In rngDB
                            .Hyperlinks.Add Anchor:=rng, Address:=rng.Value
                        Next rng
                        .Columns.AutoFit
                    End With
                End If
            End With
    End Sub
    Function SearchFolder(strRoot As String)
        Dim FS As Object
        Dim fsFD As Object
        Dim f As Object
    
        Dim colFile As Collection
        Dim p As String
    
        On Error Resume Next
        p = Application.PathSeparator
        If Right(strRoot, 1) = p Then
        Else
            strRoot = strRoot & p
        End If
    
        Set FS = CreateObject("Scripting.FileSystemObject")
        Set fsFD = FS.GetFolder(strRoot)
        Set colFile = New Collection
        For Each f In fsFD.Files
            colFile.Add f.Path
        Next f
    
        SearchSubfolder colFile, fsFD
    
        Set SearchFolder = colFile
        Set fsFD = Nothing
        Set FS = Nothing
        Set colFile = Nothing
    
    End Function
    Sub SearchSubfolder(colFile As Collection, objFolder As Object)
        Dim sbFolder As Object
        Dim f As Object
        For Each sbFolder In objFolder.subfolders
            SearchSubfolder colFile, sbFolder
            For Each f In sbFolder.Files
                colFile.Add f.Path
            Next f
        Next sbFolder
    
    End Sub
    

提交回复
热议问题