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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,216,185
Messages
6,129,388
Members
449,506
Latest member
nomvula

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