Macro > Folder > Look Inside Multiple Workbooks > Combine Tabs with exact name match to one worksheet

algere

New Member
Joined
Aug 6, 2013
Messages
26
Does anyone know how to do this? I would like to keep the file name the data came from in the first column of the combined worksheet.

If possible, I would like to skip blank rows but insert one blank row between combined worksheets or export worksheet name with each line of exported data.

The tab names will always be:

CBOE - Labor
Labor
Labor Ledger Costs
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
If I understand you correctly you want to do the following:
  • Loop through all the workbooks in a folder
  • If the workbook contains a Tab with the same name as the tab in the summary book, then copy a specific line of data to the tab in summary workbook and include the name of the file in column A (add to bottom of list, sip row)

This is not difficult to do, but which line is to be copied from the workbooks? The last in the tab?
 
Upvote 0
Hi sijpe, I would like to merge every worksheet with a name from the aforementioned list from a location of my choosing; from first cell (A1) till last cell on Worksheet.
 
Upvote 0
try this on a copy of your workbook, and let me know what needs changing:
Rich (BB code):
Option Explicit


Sub GetData()
    Dim wbIn As Workbook, wbOut As Workbook
    Dim rIn As Range, rOut As Range
    Dim wsIn As Worksheet, wsOut As Worksheet
    Dim sPath As String, strExtension As String
    Dim diaFolder As FileDialog
    Dim lCount As Long


    Set wbOut = ThisWorkbook
    
    ' Open the file dialog to get the folder for the files
    Set diaFolder = Application.FileDialog(<wbr>msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show
  
    sPath = diaFolder.SelectedItems(1)
    If Right(sPath, 1) <> "\" Then
        sPath = sPath & "\"
    End If
    'Change extension
    ChDir sPath
    'get the first file name
    strExtension = Dir("*.xls*")
     
    'Speed up processing by disabling screen updating and events
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    
    'Loop through all the books
    Do While strExtension <> ""
        Set wbIn = Workbooks.Open(sPath & strExtension)
     
        'loop through all the sheets in the opened book
        For Each wsIn In wbIn.Sheets
            'check if name is in our list
            Select Case wsIn.Name
                Case "CBOE - Labor", "Labor", "Labor Ledger Costs"
                    'yes, now select the same named sheet in our summary WB
                    Set wsOut = wbOut.Sheets(wsIn.Name)
                    'set output range to last row
                    Set rOut = wsOut.Cells(wsOut.Rows.Count, 1).End(xlUp).Offset(1, 0)
                    'now copy the values accross
                    With wsIn.Range("A1").CurrentRegion
                        rOut.Resize(.Rows.Count, Columns.Count).Value = .Value
                    End With
                
             End Select
        Next wsIn
        'close WB
        wbIn.Close savechanges:=False
        'Get next WB name
        strExtension = Dir
    Loop
 
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    
    'Cleanup
    Set wbIn = Nothing
    Set wbOut = Nothing
    Set rIn = Nothing
    Set rOut = Nothing
    Set wsIn = Nothing
    Set wsOut = Nothing
    Set diaFolder = Nothing
End Sub
 
Upvote 0
Hi
Could you please help me.
I have 17 files that have 9 sheets each that I want consolidated into a master file. however, I want the sheets with the same name to be consolidated together ( all the work books have exactly the same sheet names). So the end result will give me one workbook with 9 sheet that contains all the data from the 17 files that I have in that folder.

Also the folder will be different every month so It would be better if I can choose the files every time I run the macro.

If you can modify the VBA you posted before to suit, that be great help. this could save hours of copy and pasting every month.

Your help is much appreciated in advance.

thanks
 
Upvote 0
it would be something like this. Note the comment at the top and modify the path

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><br><SPAN style="color:#007F00">' >>>>> Put the initial path where the files to be processed are stored here. _<br>  End with backslash</SPAN><br><SPAN style="color:#00007F">Const</SPAN> sInitialPath = "C:\MyPath\"<br><br><br><SPAN style="color:#00007F">Sub</SPAN> GetData()<br>    <SPAN style="color:#00007F">Dim</SPAN> wbIn <SPAN style="color:#00007F">As</SPAN> Workbook, wbOut <SPAN style="color:#00007F">As</SPAN> Workbook<br>    <SPAN style="color:#00007F">Dim</SPAN> rIn <SPAN style="color:#00007F">As</SPAN> Range, rOut <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> wsIn <SPAN style="color:#00007F">As</SPAN> Worksheet, wsOut <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> diaFolder <SPAN style="color:#00007F">As</SPAN> FileDialog<br>    <SPAN style="color:#00007F">Dim</SPAN> lCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><br><br>    <SPAN style="color:#00007F">Set</SPAN> wbOut = ThisWorkbook<br>    <br>        <SPAN style="color:#007F00">'   get file name for file to process</SPAN><br>    MsgBox "Select all the files you want to process by using the Ctrl key and the mouse. "<br><br>    <SPAN style="color:#007F00">' Open the file dialog to get the  files</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> diaFolder = Application.FileDialog(msoFileDialogFilePicker)<br>    <SPAN style="color:#00007F">With</SPAN> diaFolder<br>        .AllowMultiSelect = <SPAN style="color:#00007F">True</SPAN><br>        .InitialView = msoFileDialogViewList<br>        .InitialFileName = sInitialPath<br>        lCount = .Show<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> lCount = -1 <SPAN style="color:#00007F">Then</SPAN><br>    <SPAN style="color:#007F00">' for each selected file</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> lCount = 1 <SPAN style="color:#00007F">To</SPAN> diaFolder.SelectedItems.Count<br>        <br>        <SPAN style="color:#00007F">Set</SPAN> wbIn = Workbooks.Open(diaFolder.SelectedItems(lCount))<br><br>        <SPAN style="color:#007F00">'loop through all the sheets in the opened book</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> wsIn In wbIn.Sheets<br>            <SPAN style="color:#00007F">Set</SPAN> wsOut = wbOut.Sheets(wsIn.Name)<br>            <SPAN style="color:#007F00">'set output range to last row</SPAN><br>            <SPAN style="color:#00007F">Set</SPAN> rOut = wsOut.Cells(wsOut.Rows.Count, 1).End(xlUp).Offset(1, 0)<br>            <SPAN style="color:#007F00">'now copy the values accross</SPAN><br>            <SPAN style="color:#00007F">With</SPAN> wsIn.Range("A1").CurrentRegion<br>                rOut.Resize(.Rows.Count, Columns.Count).Value = .Value<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> wsIn<br>        <SPAN style="color:#007F00">'close WB</SPAN><br>        wbIn.Close savechanges:=<SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> lCount<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br><br>    <SPAN style="color:#007F00">'Cleanup</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wbIn = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wbOut = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rIn = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> rOut = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsIn = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsOut = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> diaFolder = <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
hi Sijpie
thanks for your reply.
When I run the macro I get an error in "Set wsOut = wbOut.Sheets(wsIn.Name)".

do you know what the problem is?

not sure if this matters but all the worksheets are password protected but the users can copy data from all the sheets and paste them to other workbooks.

thanks
 
Last edited:
Upvote 0
The macro expects the same sheet names in the consolidation workbook and the data workbooks. So it loops through each worksheet wsIn in the data workbook (wbIn) and sets the output worksheet wsOut to the same named sheet as wsIn.

I thought that was what you were trying to achieve.
 
Upvote 0
Hi
All the sheet names are the same, but I still get an error. Do I have to create a master workbook with the same sheet names first then run the macro?

Actually, if it's not too much trouble for u and to make it easier, would it be possible to modify the code so all the data from all the sheets in the 17 files be copied one after the other in an existing master sheet.

So I would run the macro in a sheet called master.

In addition, every time macro is run I want it to paste the data starting on the first cell in the next empty column

I know I'm asking for too much, but if you can do this to for me, I would save about 4-5 hours a week.


Thanks in advance
 
Upvote 0
This us exactly what the code is supposed to be doing. The macros are supposed to be in your master workbook, the name can be anything you like. When you run the macro (from the master WB) it will first ask you to identify the workbooks to be imported. Then it will open up each of these workbooks in turn and copy the data held in each sheet to the sheet with the same name in the master workbook.

So once again:
The macro requires:
-Workbooks with sheet names
-A master workbook with the same sheet names
-The macros to be in the master workbook
 
Upvote 0

Forum statistics

Threads
1,217,408
Messages
6,136,436
Members
450,011
Latest member
faviles5566

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