Combing multiple files into 1 - use file names as tab names

therealjdj

New Member
Joined
Jul 8, 2013
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hey everyone,
I am trying to combing multiple excel files into one larger file. Each original file only has one tab. I have searched and found code to do this, however each solution ends up with tabs names Sheet 1, Sheet 1 (2), Sheet 1 (3),...
I really need the tabs to be named per the original file names.

Here is the code I have. What do I need to add to automatically rename the tabs when they are created using the original file name? Thank you!!

Code:
Sub MergeMe()

Dim Path As String
Dim Filename As String
Dim Sheet As Worksheet

Path = "C:\Data"
Filename = Dir(Path & "*.xlsx")
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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hey, a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
        Const P = "C:\Data\"
          Dim F$
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    With ThisWorkbook.Sheets
        While .Count > 1:  .Item(.Count).Delete:  Wend
              F = Dir$(P & "*.xlsx")
        While F > ""
            Workbooks.Open P & F, 0
            ActiveWorkbook.ActiveSheet.Copy , .Item(.Count)
           .Item(.Count).Name = Left(ActiveWorkbook.Name, Application.Min(31, Len(ActiveWorkbook.Name) - 5))
            ActiveWorkbook.Close
              F = Dir$
        Wend
    End With
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
Hmm this doesn't seem to work for me.
All it does is open the first workbook in the identified folder and then it stops.
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
Sub MergeMe()
    
    Dim FilePath    As String
    Dim FileName    As String
    Dim wb          As Workbook
    
    FilePath = "C:\Data"
    FileName = Dir(FilePath & "*.xlsx", vbDirectory)
    
    On Error GoTo myerror
    Application.ScreenUpdating = False
    Do While FileName <> ""
        
        Set wb = Workbooks.Open(FilePath & FileName, False, True)
        wb.Sheets(1).Copy After:=ThisWorkbook.Sheets(1)
        ActiveSheet.Name = Left(Mid(FileName, 1, Len(FileName) - 5), 31)
        wb.Close False
        
        FileName = Dir()
        Set wb = Nothing
    Loop
    
myerror:
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
    
End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,215,836
Messages
6,127,173
Members
449,368
Latest member
JayHo

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