Insert to a workbook - every files selected

LFKim2018

Active Member
Joined
Mar 24, 2018
Messages
267
I have multiple workbooks in a directory and each with only 1 worksheet.
I wish to insert those sheets (including all formats) from every workbook to a master file.
If it is possible to use a file dialog box where I can just select the files to insert - the better.
(else, the usual identifying the path method inside the code - which is quite tedious).
just copypaste all sheets - no need to rename (I have a separate module for renaming the sheets).
I searched for some codes from googling around - but cannot complete the process.
pls help with the VBA..
many many thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I found a snippet - kindly help in the copying of sheets into the master workbook.
many many thanks

Code:
Sub CopySheetsToMasterfile()
    Dim xRow As Long
    Dim xDirect$, xFname$, InitialFoldr$
    InitialFoldr$ = "C:\"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Select a FOLDER to copy Files from"
        .InitialFileName = InitialFoldr$
        .Show
[I][B]        *** insert here the code to copy each sheets into the master file (many thanks)
[/B][/I]        [I][B]*** insert all files (sheet) in the selected folder[/B][/I]
    End With
End Sub
 
Last edited:
Upvote 0
I found the code.
I am sharing this to anyone who might need it.
thanks..


Code:
Sub [COLOR=#574123]CopySheetsToMasterfile()[/COLOR]
    Dim fnameList, fnameCurFile As Variant
    Dim countFiles As Integer
    Dim wksCurSheet As Worksheet
    Dim wbkCurBook, wbkSrcBook As Workbook
    fnameList = Application.GetOpenFilename(FileFilter:="Microsoft Excel Workbooks   (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm", _
                Title:="Choose Excel files to merge", MultiSelect:=True)
    If (vbBoolean <> VarType(fnameList)) Then
        If (UBound(fnameList) > 0) Then
            countFiles = 0
            Application.ScreenUpdating = False
            Application.Calculation = xlCalculationManual
            Set wbkCurBook = ActiveWorkbook
            For Each fnameCurFile In fnameList
                countFiles = countFiles + 1
                Set wbkSrcBook = Workbooks.Open(Filename:=fnameCurFile)
                For Each wksCurSheet In wbkSrcBook.Sheets
                    wksCurSheet.Copy after:=wbkCurBook.Sheets(wbkCurBook.Sheets.Count)
                Next
                wbkSrcBook.Close SaveChanges:=False
            Next
            Application.ScreenUpdating = True
            Application.Calculation = xlCalculationAutomatic
            MsgBox "Processed " & countFiles & " files.."
        End If
    Else
        MsgBox "No files selected", Title:="Merge Excel files"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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