Tweaking a Macro to combine spreadsheets

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

:coffee:
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Ws.Name = WS.[C7]
should do the job for you. Just place after the line "For each Ws etc"
HTH (y)
 
Upvote 0
I would like to use the macro posted here to combine multiple excel files into one workbook with multiple tabs. Fortunately mine are all named differently.

Where do I start to use this macro? I need basic help from square 1 on using this macro.

Thanks
 
Upvote 0
Open a brand new workbook with just the one blank worksheet in it (ideally) or else the workbook you wish to copy the others into.
Right click on a sheet tab and select 'View Code', OR press Alt+F11
In the Project window, top left of the 3, right click on any element and choose 'Insert' and from the subsequent dialogue box select 'Module'
Now select all of the code below and press Ctrl+C to copy the code to the clipboard
Back to the new module and paste the code in.
Make sure that the Path is the full path for your collection of files and the files are all in the same place.
Place the insertion point of the cursor(flashing cursor) anywhere within the code and then press F5 to run the macro.
Any problems then post back.

Code:
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 = "D:\My Documents\Excel Problems\Holiday" '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
(y)
 
Upvote 0

Forum statistics

Threads
1,203,266
Messages
6,054,455
Members
444,727
Latest member
Mayank Sharma

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top