Copy multiple sheets from one workbook to multiple files in a folder

TK289

New Member
Joined
Oct 18, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hi all,

I am working on a project where I need to transfer multiple sheets from one standardized workbook to multiple files within a folder. This will continue throughout sub-folders within the folder I've selected.
I've managed to get this far but I am not sure how to fix the last bit. Not sure how to move/copy multiple sheets onto active file that is open. Tried to use activeworkbook but that doesn't work...
Is there any way to fix this code slightly to make it work?

p.s. I want all the sheets pasting to be after sheet "First".


VBA Code:
Private Sub Open_Workbooks_In_Folder(folderPath As String, matchWorkbooks As String)

    Static FSO As Object
    Dim thisFolder As Object, subfolder As Object
    Dim thisFile As Object
    Dim wb As Workbook
    
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Set thisFolder = FSO.GetFolder(folderPath)
    Application.DisplayAlerts = False
    For Each thisFile In thisFolder.Files
        If LCase(thisFile.Name) Like LCase(matchWorkbooks) Then
        
    Workbooks(1).Activate
    Sheets(Array("A", "B", _
        "C", "D", _
        "E", "F", "G", _
        "H", "I")).Select
        Workbooks(2).Activate
    Sheets(Array("A", "B", _
        "C", "D", _
        "E", "F", "G", _
        "H", "I")).Copy After:=ActiveWorkbook.Sheets("First")
  
            wb.Close SaveChanges:=True

            DoEvents
        End If
    Next
    Application.DisplayAlerts = True
    'Do subfolders
    
    For Each subfolder In thisFolder.SubFolders
        Open_Workbooks_In_Folder subfolder.Path, matchWorkbooks
    Next
    
End Sub

Public Sub Open_All_Workbooks_In_Folders()
       
    Dim FldrPicker As FileDialog

    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
        
        .Title = "Select A Target Folder"
        .AllowMultiSelect = False
        
        If .Show Then
            
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            
            Open_Workbooks_In_Folder .SelectedItems(1), "*.xlsx"
            
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            
            MsgBox "Done"
        
        End If
End With

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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