Macro to combine multiple worksheets into a summary workbook without merging the worksheets

treyabrown

New Member
Joined
May 23, 2014
Messages
7
I am trying to figure out how to combine 2 macros. The first macro allows me to select the file path I need, and the second macro will use the file path from the first macro and pull the worksheets from all files in the file path and combine them into 1 summary workbook.

What is the best way to combine the 2 macros, OR call the variable from the first macro?

The first macro:
Sub GetFolder()
Set myFolder = Application.FileDialog(msoFileDialogFolderPicker)
With myFolder
.Title = "Choose Folder"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
FolderSelected = .SelectedItems(1)
End With
MsgBox "You Selected:" & FolderSelected
End Sub

Second macro:
Sub GetSheets()
Path = "Y:\MC+ Data Dumps" & "\"
Filename = Dir(Path & "*.xls")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this...

Code:
[color=darkblue]Sub[/color] GetSheets()
    
    [color=darkblue]Dim[/color] strPath [color=darkblue]As[/color] [color=darkblue]String[/color], Filename [color=darkblue]As[/color] [color=darkblue]String[/color], ws [color=darkblue]As[/color] Worksheet, counter [color=darkblue]As[/color] [color=darkblue]Long[/color]
    
    [color=darkblue]With[/color] Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Choose Folder"
        .AllowMultiSelect = [color=darkblue]False[/color]
        [color=darkblue]If[/color] .Show <> -1 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
        strPath = .SelectedItems(1) & "\"
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    Application.ScreenUpdating = [color=darkblue]False[/color]
    Filename = Dir(strPath & "*.xls")
    [color=darkblue]Do[/color] [color=darkblue]While[/color] Filename <> ""
        [color=darkblue]With[/color] Workbooks.Open(Filename:=strPath & Filename, ReadOnly:=True)
            counter = counter + 1
            [color=darkblue]For[/color] [color=darkblue]Each[/color] ws [color=darkblue]In[/color] .Worksheets
                ws.Copy After:=ThisWorkbook.Sheets(1)
            [color=darkblue]Next[/color] ws
            ActiveWorkbook.Close SaveChanges:=[color=darkblue]False[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        Filename = Dir
    [color=darkblue]Loop[/color]
    Application.ScreenUpdating = [color=darkblue]True[/color]
    
    MsgBox counter & " files merged. ", vbInformation, "File Merge Complete"
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,375
Members
448,955
Latest member
BatCoder

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