AsparagusLady
New Member
- Joined
- Dec 16, 2016
- Messages
- 9
Hi all, very new to VB and would appreciate some help!
trying to copy a worksheet into 219 different workbooks. I've pilfered the code below from an old post (08) on this site but I'm getting 'Run-time error 9 : Subscript out of range'.
Can anyone help please?
---
Option Explicit
Public Sub CopySheetToAllWorkbooksInFolder()
Dim sourceSheet As Worksheet
Dim folder As String, filename As String
Dim destinationWorkbook As Workbook
'Worksheet in active workbook to be copied as a new sheet to the 219 workbooks
Set sourceSheet = ActiveWorkbook.Worksheets("16-17")
'Folder containing the 219 workbooks
folder = "\\ash-dc01\FolderRedirection\DHendricksen\Desktop\MacroTest\"
filename = Dir(folder & "*.xls", vbNormal)
While Len(filename) <> 0
Debug.Print folder & filename
Set destinationWorkbook = Workbooks.Open(folder & filename)
sourceSheet.Copy before:=destinationWorkbook.Sheets(1)
destinationWorkbook.Close True
filename = Dir() ' Get next matching file
Wend
End Sub
trying to copy a worksheet into 219 different workbooks. I've pilfered the code below from an old post (08) on this site but I'm getting 'Run-time error 9 : Subscript out of range'.
Can anyone help please?
---
Option Explicit
Public Sub CopySheetToAllWorkbooksInFolder()
Dim sourceSheet As Worksheet
Dim folder As String, filename As String
Dim destinationWorkbook As Workbook
'Worksheet in active workbook to be copied as a new sheet to the 219 workbooks
Set sourceSheet = ActiveWorkbook.Worksheets("16-17")
'Folder containing the 219 workbooks
folder = "\\ash-dc01\FolderRedirection\DHendricksen\Desktop\MacroTest\"
filename = Dir(folder & "*.xls", vbNormal)
While Len(filename) <> 0
Debug.Print folder & filename
Set destinationWorkbook = Workbooks.Open(folder & filename)
sourceSheet.Copy before:=destinationWorkbook.Sheets(1)
destinationWorkbook.Close True
filename = Dir() ' Get next matching file
Wend
End Sub