+ Reply to Thread
Results 1 to 3 of 3

How to make this code search subfolders??

Hybrid View

windowshopr How to make this code search... 04-22-2014, 10:20 AM
xlbiznes Re: How to make this code... 04-22-2014, 10:38 AM
alansidman Re: How to make this code... 04-22-2014, 10:39 AM
  1. #1
    Registered User
    Join Date
    07-24-2013
    Location
    Canada
    MS-Off Ver
    Excel 2016
    Posts
    38

    How to make this code search subfolders??

    Hey guys,

    Can't remember where I got this code from, but I think it was on the excel references site, wherever that is.

    Anyway, what it does is, it searches a folder for all excel files, then copies a defined range of cells from those files using a loop function for every file in that folder, then pastes those values into a new workbook. This works great, but what I would like it to do is be able to search subfolders as well, rather than me having to define every folder within that directory. Perhaps someone knows a way to code another loop function to search for folders within the folder? Thanks for taking a look,

    Sub MergeAllWorkbooks()
        Dim MyPath As String, FilesInPath As String
        Dim MyFiles() As String
        Dim SourceRcount As Long, FNum As Long
        Dim mybook As Workbook, BaseWks As Worksheet
        Dim sourceRange As Range, destrange As Range
        Dim rnum As Long, CalcMode As Long
    
        ' Change this to the path\folder location of your files.
        MyPath = "\\MYBOOKLIVE\Public\"
    
        ' Add a slash at the end of the path if needed.
        If Right(MyPath, 1) <> "\" Then
            MyPath = MyPath & "\"
        End If
    
        ' If there are no Excel files in the folder, exit.
        FilesInPath = Dir(MyPath & "*.xl*")
        If FilesInPath = "" Then
            MsgBox "No files found"
            Exit Sub
        End If
    
        ' Fill the myFiles array with the list of Excel files
        ' in the search folder.
        FNum = 0
        Do While FilesInPath <> ""
            FNum = FNum + 1
            ReDim Preserve MyFiles(1 To FNum)
            MyFiles(FNum) = FilesInPath
            FilesInPath = Dir()
        Loop
    
        ' Set various application properties.
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        ' Add a new workbook with one sheet.
        Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
        rnum = 1
    
        ' Loop through all files in the myFiles array.
        If FNum > 0 Then
            For FNum = LBound(MyFiles) To UBound(MyFiles)
                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
                On Error GoTo 0
    
                If Not mybook Is Nothing Then
                    On Error Resume Next
    
                    ' Change this range to fit your own needs.
                    With mybook.Worksheets(1)
                        Set sourceRange = .Range("P2:Q20")
                    End With
    
                    If Err.Number > 0 Then
                        Err.Clear
                        Set sourceRange = Nothing
                    Else
                        ' If source range uses all columns then
                        ' skip this file.
                        If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                            Set sourceRange = Nothing
                        End If
                    End If
                    On Error GoTo 0
    
                    If Not sourceRange Is Nothing Then
    
                        SourceRcount = sourceRange.Rows.Count
    
                        If rnum + SourceRcount >= BaseWks.Rows.Count Then
                            MsgBox "There are not enough rows in the target worksheet."
                            BaseWks.Columns.AutoFit
                            mybook.Close savechanges:=False
                            GoTo ExitTheSub
                        Else
    
                            ' Copy the file name in column A.
                            With sourceRange
                                BaseWks.Cells(rnum, "A"). _
                                        Resize(.Rows.Count).Value = MyFiles(FNum)
                            End With
    
                            ' Set the destination range.
                            Set destrange = BaseWks.Range("B" & rnum)
    
                            ' Copy the values from the source range
                            ' to the destination range.
                            With sourceRange
                                Set destrange = destrange. _
                                                Resize(.Rows.Count, .Columns.Count)
                            End With
                            destrange.Value = sourceRange.Value
    
                            rnum = rnum + SourceRcount
                        End If
                    End If
                    mybook.Close savechanges:=False
                End If
    
            Next FNum
            BaseWks.Columns.AutoFit
        End If
    
    ExitTheSub:
        ' Restore the application properties.
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = CalcMode
        End With
    End Sub

  2. #2
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: How to make this code search subfolders??

    try this link:

    http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  3. #3
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,703

    Re: How to make this code search subfolders??

    Look at this link which was for a similar situation, but it allows for subfolder search.

    http://www.excelforum.com/excel-prog...-by-dates.html
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Search for files in folders and subfolders
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2014, 08:43 AM
  2. Filesystemobject to search subfolders
    By wazimu13 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2014, 05:02 PM
  3. [SOLVED] FSO to search until subfolders
    By zhaype in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-18-2013, 03:29 AM
  4. Replies: 0
    Last Post: 03-05-2009, 01:43 PM
  5. [SOLVED] file search in subfolders
    By Pflugs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2005, 12:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1