Consolidate all worksheets into master workbook

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
Hi All,

I think this needs a For Each loop, but I am stuck on how to get it accompished. In the folder I will have about 26 workbooks with all different titles, but the beggining is always the same for the workbook and the worksheet (Bldg). There will be only one ws per wb and the wb I am using to collect all of the worksheets is Final.xls. The code attached works fine, but the only way right now I know how to capture the next wb/ws is with a call. Can somebody help with how to loop through all wbs and return the ws?

Code:
Sub CopyAllWrksht()
    Dim MyPath As String, MyFile As String
    On Error GoTo Error_Handler
        MyPath = ActiveWorkbook.Path & "\"
        MyFile = "Bldg LBV.xls"
    If Dir(MyPath & MyFile) = Empty Then
        MsgBox "The file " & MyFile & " was not found", , "File Doesn't Exist"
    Exit Sub
    End If
        Workbooks.Open Filename:=MyPath & MyFile
        Sheets("Bldg LBV").Copy After:=Workbooks("Final.xls").Sheets(1)
        Workbooks(MyFile).Close
        Exit Sub
Error_Handler:
End Sub
 
you can arrange all the 14 wrkbooks in single folder and use the below code. It will open all sheets take data and put into master sheet. Pls replace fpath=(give your folder path)

Sub copy_to_master2()
On Error GoTo aaa
Application.ScreenUpdating = False
Dim fpath As String, f As String, sht As Worksheet, wbk As Workbook
fpath = "************Folder Path**********"
f = Dir(fpath & "*.csv")
Do While Len(f) <> 0
Set wbk = Workbooks.Open(fpath & f, False, True)
For Each sht In wbk.Worksheets
sht.UsedRange.Copy
lastrow = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row
ThisWorkbook.Sheets(1).Range("A" & (lastrow + 1)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next sht
aaa:
Workbooks(f).Close False
f = Dir()
Loop
ThisWorkbook.Activate
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
OK now i am truly confronted with the depths of my ignorance in this subject ...

I tried to create a sample data set to try out the code you gave me, but it doesn't seem to be working. Here is what I did:

- created 3 dummy workbooks, each with 3 worksheets - identitical format, names and data for the sheets
- put these 3 dummy workbooks in one folder
- opened a new workbook , saved it outside the folderand pasted the code you gave me in the Visual Basic Editor
- F5 to run the code

... but nothing happens :($

Thanks again!!
 
Upvote 0
did u put the folder path correctly, pls put in below format

fpath = "C:\Documents and Settings\paull\Desktop\MA-JUNE\"

also press alt+f8 to run macro
 
Upvote 0
Yup!
I copied the formula exactly as you had it, but changed the file path.

Are my other steps correct though? I created a new, empty workbook outside the folder, then Tools>Macro>Visual Basic Editor, then Insert>Module, which is where I pasted the code.


Sub copy_to_master2()
On Error GoTo aaa
Application.ScreenUpdating = False
Dim fpath As String, f As String, sht As Worksheet, wbk As Workbook
fpath = "C:\Documents and Settings\yang.ong\Desktop\AKAM 2009\Monthly reporting consolidation\Testing\Entities\"
f = Dir(fpath & "*.csv")
Do While Len(f) <> 0
Set wbk = Workbooks.Open(fpath & f, False, True)
For Each sht In wbk.Worksheets
sht.UsedRange.Copy
lastrow = ThisWorkbook.Sheets(1).Range("a65536").End(xlUp).Row
ThisWorkbook.Sheets(1).Range("A" & (lastrow + 1)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next sht
aaa:
Workbooks(f).Close False
f = Dir()
Loop
ThisWorkbook.Activate
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,050
Messages
6,122,868
Members
449,097
Latest member
dbomb1414

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