ferocious12
Member
Hi All,
I am copying activeworskeet in all the files in a certain folder. I want to rename the copied worksheet in the destination workbooks but it is not working.
Can you please help where I am making a mistake?
This is the code line:
ActiveWorkbook.Sheets("Actual Receipts*").Name = "Actual Receipts"
Many thanks
I am copying activeworskeet in all the files in a certain folder. I want to rename the copied worksheet in the destination workbooks but it is not working.
Can you please help where I am making a mistake?
This is the code line:
ActiveWorkbook.Sheets("Actual Receipts*").Name = "Actual Receipts"
Many thanks
Code:
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 existing workbooks
Set sourceSheet = ActiveWorkbook.Worksheets("Actual Receipts")
On Error Resume Next
'Folder containing the existing workbooks
folder = "C:\Users\xxx\Downloads\macro\Destination files\"
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)
ActiveWorkbook.Sheets("Actual Receipts").Delete
ActiveWorkbook.Sheets("Actual Receipts*").Name = "Actual Receipts"
destinationWorkbook.Close True
filename = Dir() ' Get next matching file
Wend
End Sub