VBA Script to Copy Multiple Excel Files into one excel file, putting them into Mult tabs

pmlazalde

New Member
Joined
Aug 4, 2008
Messages
5
I need your help! I have multiple excel files in one directory that I need to copy into one master excel file and copy each incoming file into a separate tab (and putting file name on tab) in the master excel file. I do have the below VBA macro but it really doesn't work correctly. It seems to randomly make duplicate copies of some files and doesn't put name on the tab correctly on some files as well. Its not a reliable script. Can someone please help! I would appreciate it very much! Thanks in advance for looking at this for me. Phil


Sub Copyfiles()
Path = "C:\Users\Phil\Desktop\Matrices - Access\"
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
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this:
Code:
Sub CopyFiles()

    Dim path As String, fileName As String
    Dim ws As Worksheet
    
    path = "C:\Users\Phil\Desktop\Matrices - Access\"
    
    fileName = Dir(path & "*.xls")
    Do While fileName <> ""
        Workbooks.Open fileName:=path & fileName, ReadOnly:=True
        For Each ws In ActiveWorkbook.Sheets
            ws.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
            ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name = fileName & " " & ws.Name
        Next
        Workbooks(fileName).Close
        fileName = Dir()
    Loop
    
End Sub
 
Upvote 0
Hi John,

That did it!! Thanks so much! I truly appreciate your time in doing this for me. Thanks again very much!!! :-)
 
Upvote 0
Good One,

Path of the file keeps changing regularly, how can we make to path to selected one ?
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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