VBA extract sheet data from multiple subfolders into master

devofish

Board Regular
Joined
Dec 10, 2016
Messages
68
Hello all. I've been searching for several days and trying different techniques with no apparent success, so I'm hoping someone can see what I have and have a better solution. The most comprehensive, succinct, and successful code I've been able to construct with the help of many, is the example below, however, the code doesn't look in subfolders and there are a couple of tweaks that I haven't been able to configure.

My data sets are ASCII coded (basically you can see all data is read within column 1). All data are space delimited. The header information can remain intact, but once transferred it needs to autofill to lastrow of each relative data set. I've got some code that can delimit out data sets and remove the ASCII headers, so I think I can deal with delimiting it after all the files are transferred, but why isn't it looping through the sub-directories?
Code:
[COLOR=#333333]Sub Consolidate()[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">
    Dim Fs As Object 'FileSystem
    Dim d As Object 'Folder
    Dim Fx As Object 'Subfolder
    Dim file As Object 'File
    Dim PathName As String
    Dim iRow As Long 'next available row index of destination worksheet
    Dim LastRow As Long 'last row of source worksheet
    Dim wbSource As Workbook, wsMaster As Worksheet
    
    Set wsMaster = ThisWorkbook.Sheets("Master") 'sheet data will be compiled into

    With wsMaster 'data destination worksheet
        Set Fs = CreateObject("Scripting.FileSystemObject")
        Set d = Fs.GetFolder("Z:\Operations\Chupacabra\Data\")
        iRow = 2
        
        For Each Fx In d.SubFolders 'loop through subfolders
            For Each file In Fx.Files 'loop through files
                If file.Name Like "*dq1000d.las*" Then
                    PathName = Fx.Name & "\" & file.Name
                    Workbooks.Open d.Path & "\" & PathName
                    Application.StatusBar = "Processing " & PathName
                    LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1 'Find & copy to last row having data in column A
                    Range("A132:A" & LastRow).Copy .Range("A" & iRow)    'this original data is space delmited
                    Range("A13").Copy .Range("B" & iRow)                 'header data needs to remain intact & autofill
                    Range("A12").Copy .Range("C" & iRow)                 'header data needs to remain intact & autofill
                    Range("A23").Copy .Range("D" & iRow)                 'header data needs to remain intact & autofill
                    iRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1   'Next row
                    ActiveWorkbook.Close savechanges:=False
                End If
                iRow = iRow + LastRow
            Next file
        Next Fx
    End With </code>[COLOR=#333333]End Sub[/COLOR]

 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top