consolidating multiple wkbks as wkshts

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have two folders, each contain 164 workbooks. All workbooks are identical in structure and I'd like to consolidate into one workbook as multiple worksheets so that I can do all the necessary summaries.

Is it possible to pick up the lot and drop them into one workbook?

Cheers,
Jon
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Jon,

Following code uses folder path as variable - that you need to assign according to your workbooks path - and copies first worksheet from each workbook into a single workbook and rename the worksheets as their parent's name (source workbook name).

Open a new workbook then goto VBA (Alt+F11), insert a new Module (Insert->Module) and copy&paste following code into this new module that will be displayed in the right pane.

Code:
Sub CombineWorkbooks()
Dim fso As Object
Dim fld As Object
Dim fil As Object
Dim src As Workbook
Dim wrk As Workbook
Dim sht As Worksheet
Dim fldPath As String

  '*******CHANGE FOLLOWING BY USING YOUR PATH*******
  fldPath = "E:\MyWorkbooks\"
  '**************************************************

  On Error GoTo ErrHandler
  
  'Create FileSystemObject - We will use FSO to handle files
  Set fso = CreateObject("Scripting.FileSystemObject")
  'Folder contains workbooks
  Set fld = fso.getfolder(fldPath)
  
  'Create New Workbook
  Set wrk = Application.Workbooks.Add
  'Remove empty worksheets - except one
  Application.DisplayAlerts = False
  Do Until wrk.Worksheets.Count = 1
    DoEvents
    wrk.Worksheets(1).Delete
  Loop
  
  Application.ScreenUpdating = False
  'Loop through files in folder
  For Each fil In fld.Files
    'Execute only excel files
    If fil.Type = "Microsoft Excel Worksheet" Then
      'Open workbook
      Set src = Application.Workbooks.Open(fil.Path)
      'Copy first worksheet to the new excel workbook - as the last worksheet
      src.Worksheets(1).Copy After:=wrk.Worksheets(wrk.Worksheets.Count)
      Set sht = ActiveSheet
      'Rename the copied worksheet as parent workbook name
      sht.Name = src.Name
      'Close the source workbook
      src.Close False
    End If
  Next fil
  
  'Delete the first empty worksheet
  wrk.Worksheets(1).Delete

ErrHandler:
  If Err Then
    MsgBox Err.Number & "-" & Err.Description, vbOKOnly + vbExclamation, "Error"
  End If
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
End Sub

I hope it helps.
 
Upvote 0
It's running now, I imagine it'll take a little while.

Thanks a trillion, you've saved me hours!!!!

:biggrin:
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,290
Members
449,149
Latest member
mwdbActuary

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