Multiple Workbooks to one.

Claire Jackson

Board Regular
Joined
Jun 30, 2020
Messages
79
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have multiple workbooks with weekly tabs where users add data (basically a spreadsheet that captures an employees name and what work he is doing that day for a full week per tab). Each Manager uses separate spreadsheets but I want to be able to print a list of each manager's entries on one single piece of paper, so I found the below on the web but although it brings the data in, it still puts it on separate sheets. Is there a way of combining the identically labelled tabs into just one sheet?

Sub mergeFiles()

'Merges all files in a folder to a main file.
'Define variables:

Dim numberOfFilesChosen, i As Integer
Dim tempFileDialog As FileDialog
Dim mainWorkbook, sourceWorkbook As Workbook
Dim tempWorkSheet As Worksheet

Set mainWorkbook = Application.ActiveWorkbook
Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
'Allow the user to select multiple workbooks

tempFileDialog.AllowMultiSelect = True
numberOfFilesChosen = tempFileDialog.Show
'Loop through all selected workbooks

For i = 1 To tempFileDialog.SelectedItems.Count
'Open each workbook

Workbooks.Open tempFileDialog.SelectedItems(i)

Set sourceWorkbook = ActiveWorkbook
'Copy each worksheet to the end of the main workbook

For Each tempWorkSheet In sourceWorkbook.Worksheets
tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)

Next tempWorkSheet
'Close the source workbook
sourceWorkbook.Close

Next i

End Sub
 
Have you considered skipping the VBA and using Power Query. If the workbooks are formatted consistently (structure and name) it is pretty straight forward.
Here is a great video on this from MyOnlineTrainingHub:

 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi
Try this update

VBA Code:
Sub mergeFiles()
    
    'Merges all files in a folder to a main file.
    'Define variables:
    Dim i               As Long
    Dim sourceWorkbook  As Workbook
    Dim rngData         As Range
    Dim ExcludeHeader   As Boolean
    Dim tempWorkSheet   As Worksheet, mainsheet As Worksheet
    
    Dim tblFiles        As ListObject
    Dim arrManagerFiles As Variant
    
    Set tblFiles = Worksheets("Manager Files").ListObjects(1)
    
    arrManagerFiles = tblFiles.DataBodyRange.Value
    
    '----------------------------------------------------------------------------------------------
    ' SETTINGS
    '----------------------------------------------------------------------------------------------
    'master sheet
    Set mainsheet = ThisWorkbook.Worksheets("Sheet1")
    
    'set True to exclude row 1 header row
    ExcludeHeader = True
    
    '----------------------------------------------------------------------------------------------
    
    On Error GoTo myerror
    
    Application.ScreenUpdating = False
    'Loop through all selected workbooks
    
    'clear previous manager records
    mainsheet.UsedRange.Offset(1).ClearContents
    
    For i = 1 To UBound(arrManagerFiles, 1)
        'Open each workbook
        Set sourceWorkbook = Workbooks.Open(arrManagerFiles(i, 1), 0, True)
        
        'Copy each worksheet to main worksheet
        For Each tempWorkSheet In sourceWorkbook.Worksheets
            Set rngData = tempWorkSheet.UsedRange
            If ExcludeHeader Then Set rngData = rngData.Offset(1).Resize(rngData.Rows.Count - 1)
            rngData.Copy mainsheet.Cells(mainsheet.Cells(mainsheet.Rows.Count, "A").End(xlUp).Row + 1, 1)
            Set rngData = Nothing
        Next tempWorkSheet
        
        'Close the source workbook
        sourceWorkbook.Close False
        Set sourceWorkbook = Nothing
    Next i
    
myerror:
    If Not sourceWorkbook Is Nothing Then sourceWorkbook.Close False
    Application.ScreenUpdating = True
    If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

If not already done so, You will need to include the Filepath in the table

29-02-2024.xls
A
1Manager Files
2C:\Users\clairejackson\Documents\manager\file1.xlsx
Manager Files


Dave
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

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