Public Sub Copy_Sheet_To_Workbooks_In_Folder()
Dim copySheet As Worksheet
Dim folderPath As String, fileName As String
Dim destWorkbook As Workbook
Set copySheet = ActiveWorkbook.Worksheets("Sheet1") 'change sheet name or number
folderPath = "C:\path\to\folder\" 'change this
folderPath = Trim(folderPath)
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
Application.ScreenUpdating = False
fileName = Dir(folderPath & "*.xlsx")
While fileName <> vbNullString
Set destWorkbook = Workbooks.Open(folderPath & fileName)
copySheet.Copy After:=destWorkbook.Worksheets(destWorkbook.Worksheets.Count)
destWorkbook.Close saveChanges:=True
fileName = Dir
Wend
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
Thanks John,
Will try this in the weekend and post the results here.
Cheers
Public Sub Copy_Sheet_To_Workbooks_In_Subfolders()
Dim copySheet As Worksheet
Dim mainFolderPath As String
Dim destWorkbook As Workbook
Dim files As Variant
Dim i As Long
Set copySheet = ActiveWorkbook.Worksheets("Sheet1") 'change sheet name or number
mainFolderPath = "C:\path\to\main folder\" 'change this
mainFolderPath = Trim(mainFolderPath)
If Right(mainFolderPath, 1) <> "\" Then mainFolderPath = mainFolderPath & "\"
files = Split(CreateObject("WScript.Shell").Exec("cmd /c DIR " & """" & mainFolderPath & "*.xlsx" & """" & " /B /S").StdOut.ReadAll, vbCrLf)
Application.ScreenUpdating = False
For i = 0 To UBound(files) - 1
Set destWorkbook = Workbooks.Open(files(i))
copySheet.Copy After:=destWorkbook.Worksheets(destWorkbook.Worksheets.Count)
destWorkbook.Close saveChanges:=True
Next
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
Try this macro, which does the same as the previous macro, but for *.xlsx files in all subfolders and all levels from the main folder.
Code:Public Sub Copy_Sheet_To_Workbooks_In_Subfolders() Dim copySheet As Worksheet Dim mainFolderPath As String Dim destWorkbook As Workbook Dim files As Variant Dim i As Long Set copySheet = ActiveWorkbook.Worksheets("Sheet1") 'change sheet name or number mainFolderPath = "C:\path\to\main folder\" 'change this mainFolderPath = Trim(mainFolderPath) If Right(mainFolderPath, 1) <> "\" Then mainFolderPath = mainFolderPath & "\" files = Split(CreateObject("WScript.Shell").Exec("cmd /c DIR " & """" & mainFolderPath & "*.xlsx" & """" & " /B /S").StdOut.ReadAll, vbCrLf) Application.ScreenUpdating = False For i = 0 To UBound(files) - 1 Set destWorkbook = Workbooks.Open(files(i)) copySheet.Copy After:=destWorkbook.Worksheets(destWorkbook.Worksheets.Count) destWorkbook.Close saveChanges:=True Next Application.ScreenUpdating = True MsgBox "Done" End Sub
Public Sub Copy_Sheet_To_Workbooks_In_Subfolders()
Dim copySheet As Worksheet
Dim mainFolderPath As String
Dim destWorkbook As Workbook
Dim files As Variant
Dim i As Long
Set copySheet = ActiveWorkbook.Worksheets("Sheet1") 'change sheet name or number
mainFolderPath = "C:\path\to\folder\" 'change this
mainFolderPath = Trim(mainFolderPath)
If Right(mainFolderPath, 1) <> "\" Then mainFolderPath = mainFolderPath & "\"
files = Split(CreateObject("WScript.Shell").Exec("cmd /c DIR " & """" & mainFolderPath & "rapportage*.xlsx" & """" & " /B /S").StdOut.ReadAll, vbCrLf)
Application.ScreenUpdating = False
For i = 0 To UBound(files) - 1
Set destWorkbook = Workbooks.Open(files(i))
copySheet.Copy After:=destWorkbook.Worksheets(1)
destWorkbook.Close saveChanges:=True
Next
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
Just change the *.xlsx to rapportage*.xlsx. And to put the new sheet as the second sheet, use After:=destWorkbook.Worksheets(1), i.e. after the first sheet.
Code:Public Sub Copy_Sheet_To_Workbooks_In_Subfolders() Dim copySheet As Worksheet Dim mainFolderPath As String Dim destWorkbook As Workbook Dim files As Variant Dim i As Long Set copySheet = ActiveWorkbook.Worksheets("Sheet1") 'change sheet name or number mainFolderPath = "C:\path\to\folder\" 'change this mainFolderPath = Trim(mainFolderPath) If Right(mainFolderPath, 1) <> "\" Then mainFolderPath = mainFolderPath & "\" files = Split(CreateObject("WScript.Shell").Exec("cmd /c DIR " & """" & mainFolderPath & "rapportage*.xlsx" & """" & " /B /S").StdOut.ReadAll, vbCrLf) Application.ScreenUpdating = False For i = 0 To UBound(files) - 1 Set destWorkbook = Workbooks.Open(files(i)) copySheet.Copy After:=destWorkbook.Worksheets(1) destWorkbook.Close saveChanges:=True Next Application.ScreenUpdating = True MsgBox "Done" End Sub