combine workbooks of same filename in subfolders

fraufreda

Board Regular
Joined
Oct 14, 2010
Messages
190
I have workbooks of the same file name but in different folders. currently I have the code to combine ALL workbooks in subfolders and it is working. however, I tried to combine only specific filename form folders but without success.
any help would be highly appreciated

first code

Code:
Sub Import_All_CD_2()

    Dim vaFileName As Variant
    Const MyDir As String = "D:\Sales\JUN 17\"    'workbook location with trailing \

    
    With ApplicationFileSearch
        .NewSearch
        .LookIn = MyDir
        'the directory To  search In
        .SearchSubFolders = True
        .FileType = msoFileTypeExcelWorkbooks
        If .Execute > 0 Then
            'workbooks found
            Application.ScreenUpdating = False
            For Each vaFileName In .FoundFiles
                ' loop through each found workbook
                ProcessData vaFileName
                'pass workbook fullname To process routine (see below)
            Next
        Else
             MsgBox "There were no Excel files found."
        End If
        Application.ScreenUpdating = False
    End With
     
End Sub

second code

Code:
Sub ProcessData(ByVal FName As String)
On Error Resume Next
    Dim WBK As Workbook
      Dim MCDrow As Long
    MCDrow = ThisWorkbook.Sheets("Sheet1").Range("B65536").End(xlUp).Row
'here is my attempt to find only the workbooks I want but it is not working if I remove it will combine all workbooks in subfolders 
    FName = "sqlsales.xls"
'------
    Set WBK = Workbooks.Open(FileName:=FName)
    'open the target workbook
       'do your stuff below
 
        'WBK.Close savechanges:=True
        'Exit Sub
        'find row before next blank  cell in column A
        ilastrow = Range("A1").End(xlDown).Row
        Range("A1:fj" & ilastrow).Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(MCDrow + 1, 2)
        MCDrow = ThisWorkbook.Sheets("Sheet1").Range("B65536").End(xlUp).Row
        'CLEAR COLUMNS
        Sheets("Sheet1").Range("A1:C1").EntireColumn.ClearContents
        Call DeleteColumns
        Application.ScreenUpdating = True

    'MsgBox wbk.Name
    '
     
    WBK.Close savechanges:=False
    'close the workbook without  saving any  changes
     
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
When U say combine wbs do U mean the processdata routine will extract data from all the wb passed to the active wb? If so where are U going to put it. Do you want to extract whole sheets or just info from sheets? HTH. Dave
ps. what's with the clearing/deleting columns stuff
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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