gspurr
New Member
- Joined
- Jul 29, 2003
- Messages
- 30
Hi
I am trying to combine the worksheets of about 50 files in a folder into a single workbook. (each file only has one sheet)
I am using the code below (which I got from this site but can't remeber who from to credit - sorry) which works perfectly at bringing in all the cells...
Option Explicit
Sub CombineFiles()
Dim Path As String
Dim FileName As String
Dim Wkb As Workbook
Dim WS As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
Path = "C:\" 'Change as needed
FileName = Dir(Path & "\*.xls", vbNormal)
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)
For Each WS In Wkb.Worksheets
WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Next WS
Wkb.Close False
FileName = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
...my problem is that all the sheets in the workbooks in the folder have the same name and when copied into the new workbook keep the same name which seems to make the workbook unstable so that even if I try and rename manually it falls over and crashes excel.
What I would like to do ideally is tweak this macro so that before each sheet is copied into the new workbook, it is renamed to the value in cell C7 which is unique.
I am sure this should be simple but I can;t make it work!!!!!
Please help
Thanks
Gemma

I am trying to combine the worksheets of about 50 files in a folder into a single workbook. (each file only has one sheet)
I am using the code below (which I got from this site but can't remeber who from to credit - sorry) which works perfectly at bringing in all the cells...
Option Explicit
Sub CombineFiles()
Dim Path As String
Dim FileName As String
Dim Wkb As Workbook
Dim WS As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
Path = "C:\" 'Change as needed
FileName = Dir(Path & "\*.xls", vbNormal)
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)
For Each WS In Wkb.Worksheets
WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Next WS
Wkb.Close False
FileName = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
...my problem is that all the sheets in the workbooks in the folder have the same name and when copied into the new workbook keep the same name which seems to make the workbook unstable so that even if I try and rename manually it falls over and crashes excel.
What I would like to do ideally is tweak this macro so that before each sheet is copied into the new workbook, it is renamed to the value in cell C7 which is unique.
I am sure this should be simple but I can;t make it work!!!!!
Please help
Thanks
Gemma