Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50085

Macro to combine many workbooks into one

$
0
0
Hello,

I have a number of workbooks (over 300) each with one sheet used as a daily total tabulation. I would like to combine all the workbooks into a single Master workbook where each is now a seperate worksheet. I have a Macro written that I feel should do what I am looking for but when it runs it loops 3 times and then comes up with a Run-time error '1004' "Cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic."

It renames the first sheet to the name of the file "Daily Report", the second to "Daily Report (2)" and the third to "Daily Report ( (3" and then runs into the error. I don't understand the reason for the last one to become "( (3)" and not "(3)"

Here is the code I am trying to use:

Sub GetSheets()
Path = "y:\DailyTotals\"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub

The debug indicates the line error is:

Sheet.Copy After:=ThisWorkbook.Sheets(1)

Thanks for any help you can give,

DZ

Viewing all articles
Browse latest Browse all 50085

Trending Articles