Combine Multiple Individual Sheets to one workbook

rambostine202

New Member
Joined
Sep 5, 2017
Messages
4
Hello people,

I am totally new here and have decent knowledge of excel and creating a MACRO at basic levels. I am not familiar with how to properly formulate my request, but I will try. If someone could please help me solve this I would be grateful.

I managed to write something to convert around 50 Text files (.txt) to .xlsx type.
So now I have 50 .xlsx files/individual sheets that I want to merge into one Excel Workbook that contains the 50 sheets. NOT combine the 50 individual sheets on 1 sheet in a workbook, but have 50 sheets in just one workbook.

for ease I am renaming them simply by selecting all, the individual xlsx files in one folder just as follows (they are all in the same folder)
-1(1).xlsx
-1(2).xlsx
-1(3).xlsx
...
-1(50).xlsx

So I have 50 files that I will generate multiple times 50 files in txt document and then have to merge in one Workbook with 50 tabs/sheets.

Can someone please guide me.

Thanks in advance.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
.
Give this a try :

Code:
Option Explicit


Sub MergeMultiSheets()


    Dim FilePath, Path As String
    Dim FileName As String
    Dim Wkb As Workbook
    Dim WS As Worksheet
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    FilePath = GetFile("C:\")
    Path = Left(FilePath, InStrRev(FilePath, "\"))
    FileName = Dir(Path & "\*.xlsx", 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


    Application.DisplayAlerts = False
        Worksheets("Sheet1").Delete
    Application.DisplayAlerts = True
End Sub


Function GetFile(strPath As String) As String


    Dim File As FileDialog
    Dim sItem As String
    Set File = Application.FileDialog(msoFileDialogFilePicker)
    With File
        .Title = "Select a File"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Excel files only", "*.xls; *.xlsx"
        .InitialFileName = strPath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFile = sItem
    Set File = Nothing


End Function

Paste a Command Button on Sheet 1 of your Masterworkbook connected to the macro.
When the FileDialog is shown, move to the folder holding all of the .xlsx files. Select one of those
files and press OK. No need to select more than one of the .xlsx files.

Be certain only the .xlsx files you want copied are in the folder and nothing else.
 
Upvote 0
Logit, you Sir are TREMENDOUSLY AWESOME!

Exactly what I needed. I spent soo much time on google and youtube trying to find this but couldn't get exactly what I wanted.

Words cannot express my gratitude.

May you always attain what it is you seek.

This is so great. Looking forward to creating all my files now.

Thank you so much.
 
Upvote 0
You are welcome. Trust it works as you need it to.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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