Merge sheet with same name from multiple workbooks.

gman23

New Member
Joined
May 10, 2016
Messages
3
Hello -

Does anyone know a VBA code that will go into the folder I specify and find the one sheet name I specify and merge into one document?


For example I have one folder with 20 excel files. I want it to go into each file find the sheet called "Variance Report" and merge all those sheets into one excel file. But each with own sheet. In the end having one excel file with 20 different "Variance Report" sheets.


Thank you in advance for any help.


Greg
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello -

Does anyone know a VBA code that will go into the folder I specify and find the one sheet name I specify and merge into one document?


For example I have one folder with 20 excel files. I want it to go into each file find the sheet called "Variance Report" and merge all those sheets into one excel file. But each with own sheet. In the end having one excel file with 20 different "Variance Report" sheets.


Thank you in advance for any help.


Greg
Hi Greg, welcome to the boards.

This is untested but I believe this should do what you describe. Put the following code in a standard module in your main workbook (the one being copied to). It should pop up with a folder picker allowing you to specify what folder to look in. You may need to change the bold red "*.xlsx" part of my code to suit whatever file extension the workbooks are saved as. Remember to prefix the file extension with the * and the . or it wont work.

Rich (BB code):
Sub LoopThroughFilesInFolder()
Dim wb As Workbook
Dim wb2 As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim x As Long
Dim FldrPicker As FileDialog


'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False


' Sets wb As the master workbook
Set wb = ThisWorkbook
' Defines variable x as 1
x = 1


'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)


With FldrPicker
    .Title = "Select A Target Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With


'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings


'Target File Extension (must include wildcard "*")
myExtension = "*.xlsx"


'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)


'Loop through each Excel file in folder
Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb2 = Workbooks.Open(Filename:=myPath & myFile)
    ' Copy Variance Report sheet to the end of the main workbook
    wb2.Sheets("Variance Report").Copy After:=wb.Sheets(Workbooks(wb).Sheets.Count)
    ' Renames the copied sheet to Variance Report and the current value of x
    wb.Sheets("Variance Report").Name = "Variance Report " & x
    ' Increases x by 1 to account for the new sheet
    x = x + 1
    'Close Workbook 2
    wb2.Close SaveChanges:=False
    'Get next file name
    myFile = Dir
Loop


'Message Box when tasks are completed
MsgBox "Task Complete!"


ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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