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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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