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:
Hi Sijpie
Thanks for that explanation. I ran the macro from a master workbook with the same sheet names. However, it is giving me an error because the sheets are password protected.

Can I start again with what I man the macro to do? My requirements have changed since I first posted the question.

What I want is:
-I want the macro to run from a master workbook that has ONE sheet.
-I have 17 files that have 9 sheets each. ( each sheet is password protected). the destination of the folder changes every time.
-I want the macro to copy the range A1 to C60 as values, from each sheet in the workbooks.
-Then paste them one after the other in the master sheet. so the result of the macro will give me three columns, with the data running down.
-As this is a master sheet, I don't want to clear the data the next time I run the macro, so the code should paste the data on the first cell on the next available column.

Could you modify the code to match the criteria above?

Please help me out here.

thanks
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
That is only a minor adjustment.


<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>    <SPAN style="color:#007F00">' Assuming masterWB has only one sheet</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsOut = wbOut.Sheets(1)<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:#007F00">'set output range on the Mastersheet 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 to the Mastersheet</SPAN><br>            <SPAN style="color:#00007F">With</SPAN> wsIn.Range("A1:C60")<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
Awesome. It works great. Just one thing. it is pasting the data one after the other. Could you modify the code so it pastes the data one the next available column. so first time I run the macro, data are pasted in columns A to C, the second time D to F and so on.....


Thanks
 
Upvote 0
Sure: modify the line where the output range is defined:
Code:
            'set output range on the Mastersheet to last column            
            Set rOut = wsOut.Cells(1,wsOut.Columns.Count,).End(xlLeft).Offset(0,1)
 
Upvote 0
Hi Sijpie
when I replace output range, it highlights the line in Red and wont let me run the macro. it is indicating there is an issue after " Count,"

Can you see what the issue is?

much appreciated your help.
 
Upvote 0
oops. remove the comma behind .Count
 
Upvote 0
Hi Sijpie
sorry, I removed the coma but it is sill giving me an error. it is highlighting "Set rOut = wsOut.Cells(1,wsOut.Columns.Count,).End(xlLeft).Offset(0,1)" in Yellow.

is code above, suppose to be pasting the data in the last column in the master sheet?

thanks
 
Upvote 0
No you didn't remove the comma, because it is still there in the line in yellow.

Delete al the code and put the following code in again, this time I corrected it:
Code:
Option Explicit




' >>>>> Put the initial path where the files to be processed are stored here. _
  End with backslash
Const sInitialPath = "C:\MyPath\"




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




    Set wbOut = ThisWorkbook
    ' Assuming masterWB has only one sheet
    Set wsOut = wbOut.Sheets(1)
    
        '   get file name for file to process
    MsgBox "Select all the files you want to process by using the Ctrl key and the mouse. "


    ' Open the file dialog to get the  files
    Set diaFolder = Application.FileDialog(msoFileDialogFilePicker)
    With diaFolder
        .AllowMultiSelect = True
        .InitialView = msoFileDialogViewList
        .InitialFileName = sInitialPath
        lCount = .Show
    End With
    If lCount = -1 Then
    ' for each selected file
    For lCount = 1 To diaFolder.SelectedItems.Count
        
        Set wbIn = Workbooks.Open(diaFolder.SelectedItems(lCount))


        'loop through all the sheets in the opened book
        For Each wsIn In wbIn.Sheets
            'set output range on the Mastersheet to last row
            Set rOut = wsOut.Cells(1,wsOut.Columns.Count,).End(xlLeft).Offset(0,1)
            'now copy the values accross to the Mastersheet
            With wsIn.Range("A1:C60")
                rOut.Resize(.Rows.Count, .Columns.Count).Value = .Value
            End With
        Next wsIn
        'close WB
        wbIn.Close savechanges:=False
    Next lCount
    End If
    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

This line is not pasting the data. What it does is it sets the range (area in the worksheet) where the values are to be copied to to the right of the last column.

It works as follows:
rOut is declared as a range. In order to set a range I use the word Set
Set rOut = Range("A1")
will set rOut to the same spot as A1
But I want to set it to the last column, one right actually. So I start from the very last column in the workbook on row 1. This is cell XFD1. Then I tell Excel to go to the End Left (just as you would do with End Left-arrow) so it stops at the last column in use in row 1. Then I tell it to take one step to the right (.Offset(0,1)).
However to avoid issues with versions of Excel, I dont use XFD1 as the starting point, because Excel 1997 has IV1 as the last column, and some futere version may have something different. So I ask Excel what the last column number is (Columns.Count). And to use this it is easier to use the term Cells() rather than Range(). So Cells(3,1) is the same as Range("A3") . and Cells(1, Columns.Count) is the same as Range("XFD1").

After I have set my output range in this way I then set the values of the output range to the same values as the input range. This is far, far faster than using copy/paste.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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