Merging multi books to multi sheets in single book

Shaner73

Board Regular
Joined
Jul 27, 2010
Messages
65
I have searched, but no luck...

I have a bunch of workbooks in a folder. I have the code to merge all workbooks together into a new workbook into one sheet.

What I need to accomplish is to merge each workbook into new workbook, but each workbook needs to go into a new worksheet/tab.

Does this make sense?

It's Friday and I've been sick for a couple weeks....so my brain is running on a skeleton crew.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I'm not too sure about what you mean by
I have a bunch of workbooks in a folder. I have the code to merge all workbooks together into a new workbook into one sheet.

So, are we only dealing with several workbooks that only have one worksheet in each of them?
 
Upvote 0
I'm not too sure about what you mean by


So, are we only dealing with several workbooks that only have one worksheet in each of them?

For this case, yes. Most of these workbook will have multiple sheets, but I save a copy and delete the sheets/tabs I won't need, leaving one sheet.
 
Upvote 0
Try
Code:
Sub RunCodeOnAllXLSFiles()
    Dim lCount As Long
    Dim wbResults As Workbook
    Dim wbCodeBook As Workbook
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    On Error Resume Next
    Set wbCodeBook = ThisWorkbook
        With Application.FileSearch
            .NewSearch
            'Change path to suit
            .LookIn = "C:\MyDocuments\TestResults"
            .FileType = msoFileTypeExcelWorkbooks
            'Optional filter with wildcard
            '.Filename = "Book*.xls"
                If .Execute > 0 Then 'Workbooks in folder
                    For lCount = 1 To .FoundFiles.count 'Loop through all
                        'Open Workbook x and Set a Workbook variable to it
                        Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        
                        Worksheets.Add
                        With ActiveSheet
                            .Name = .FoundFiles.Name
                            .Move After:=Worksheets(Worksheets.count)
                        End With
                        
                        'Make the Template sheet visible, and copy it
                        .Cells.Copy
                        Worksheets(.FoundFiles.Name).Paste
                        With Application
                            .CutCopyMode = False
                            .Goto Range("A1"), True
                            .ScreenUpdating = True
                            .EnableEvents = True
                            .DisplayAlerts = True
                        End With
                        wbResults.Close SaveChanges:=False
                    Next lCount
                End If
        End With
    On Error GoTo 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End Sub

from http://www.mrexcel.com/forum/showthread.php?t=49977
and from http://www.ozgrid.com/VBA/loop-through.htm
 
Upvote 0
Are you getting an error?

and did you change the path of the file? shown in the code
Code:
.LookIn = "C:\MyDocuments\TestResults"

What is it failing to do? (creating a worksheet, not being copied and pasted?)
 
Upvote 0
Are you getting an error?

and did you change the path of the file? shown in the code
Code:
.LookIn = "C:\MyDocuments\TestResults"
What is it failing to do? (creating a worksheet, not being copied and pasted?)

I am not getting any errors..just no results. I do get Sheet 4 added, but no data. And yes, I changed the path.
 
Upvote 0
Try
Rich (BB code):
Sub RunCodeOnAllXLSFiles()
    Dim lCount As Long
    Dim wbResults As Workbook
    Dim wbCodeBook As Workbook
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    On Error Resume Next
    Set wbCodeBook = ThisWorkbook
        With Application.FileSearch
            .NewSearch
            'Change path to suit
            .LookIn = "C:\MyDocuments\TestResults"
            .FileType = msoFileTypeExcelWorkbooks
            'Optional filter with wildcard
            '.Filename = "Book*.xls"
                If .Execute > 0 Then 'Workbooks in folder
                    For lCount = 1 To .FoundFiles.count 'Loop through all
                        'Open Workbook x and Set a Workbook variable to it
                        Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        wbResults.Sheet1.Cells.Copy 'Copies from Sheet1
                        
                        Worksheets.Add
                        With ActiveSheet
                            .Move After:=Worksheets(Worksheets.count)
                            .Paste
                        End With
                        With Application
                            .CutCopyMode = False
                            .GoTo Range("A1"), True
                            .ScreenUpdating = True
                            .EnableEvents = True
                            .DisplayAlerts = True
                        End With
                        wbResults.Close SaveChanges:=False
                    Next lCount
                End If
        End With
    On Error GoTo 0
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End Sub

I don't know as well as the mods or pros here so I don't know how to find the first visible worksheet/the worksheet you need. If it is sheet1, then it should copy and paste.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

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