Consolidate Workbooks with multiple sheets into existing worksheet

stratthaslam

Board Regular
Joined
Feb 4, 2009
Messages
63
Hello all,

Thank you in advance for your help. What I am trying to accomplish is, I have 6 workbooks with multiple sheets each all in the same folder as the destination workbook. I have been scouring the boards trying to find a macro that I can assign to a command button that will import the all info in the 6 workbooks in to one sheet of the destination workbook.

The source workbooks are

Manager Rita.xls
Manager Amy.xls
Manager Steve.xls
Manager Patricia.xls
Manager Mike.xls
Manager Jan.xls

The destination workbook is

Sr Mgr Metrics template.xls

Thank you again and I appreciate any assistance you can render.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Making changes to VBA code XL2003

I was able to find some code from Ron de Bruin on http://msdn.microsoft.com/en-us/lib...ultipleWorkbooks_MergingRangefromAllWorkbooks to merge info from multiple workbooks into a new workbook.

Can anyone help me edit this code so that it pulls all the data from the workbooks in the indicated directory and place it in a worksheet of an existing workbook instead of making a new workbook ? Thank you much for your help.

Code:
Sub MergeAllWorkbooks()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long, FNum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long
    ' Change this to the path\folder location of your files.
    MyPath = "O:\Customer Service\Team Metrics"
    ' Add a slash at the end of the path if needed.
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If
    ' If there are no Excel files in the folder, exit.
    FilesInPath = Dir(MyPath & "*.xl*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If
    ' Fill the myFiles array with the list of Excel files
    ' in the search folder.
    FNum = 0
    Do While FilesInPath <> ""
        FNum = FNum + 1
        ReDim Preserve MyFiles(1 To FNum)
        MyFiles(FNum) = FilesInPath
        FilesInPath = Dir()
    Loop
    ' Set various application properties.
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    ' Add a new workbook with one sheet.
    Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    rnum = 1
    ' Loop through all files in the myFiles array.
    If FNum > 0 Then
        For FNum = LBound(MyFiles) To UBound(MyFiles)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
            On Error GoTo 0
            If Not mybook Is Nothing Then
                On Error Resume Next
                ' Change this range to fit your own needs.
                With mybook.Worksheets(18)
                    Set sourceRange = .Range("A1:I169")
                End With
                If Err.Number > 0 Then
                    Err.Clear
                    Set sourceRange = Nothing
                Else
                    ' If source range uses all columns then
                    ' skip this file.
                    If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                        Set sourceRange = Nothing
                    End If
                End If
                On Error GoTo 0
                If Not sourceRange Is Nothing Then
                    SourceRcount = sourceRange.Rows.Count
                    If rnum + SourceRcount >= BaseWks.Rows.Count Then
                        MsgBox "There are not enough rows in the target worksheet."
                        BaseWks.Columns.AutoFit
                        mybook.Close savechanges:=False
                        GoTo ExitTheSub
                    Else
                        ' Copy the file name in column A.
                        With sourceRange
                            BaseWks.Cells(rnum, "A"). _
                                    Resize(.Rows.Count).Value = MyFiles(FNum)
                        End With
                        ' Set the destination range.
                        Set destrange = BaseWks.Range("B" & rnum)
                        ' Copy the values from the source range
                        ' to the destination range.
                        With sourceRange
                            Set destrange = destrange. _
                                            Resize(.Rows.Count, .Columns.Count)
                        End With
                        destrange.Value = sourceRange.Value
                        rnum = rnum + SourceRcount
                    End If
                End If
                mybook.Close savechanges:=False
            End If
        Next FNum
        BaseWks.Columns.AutoFit
    End If
ExitTheSub:
    ' Restore the application properties.
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub
 
Last edited by a moderator:
Upvote 0
I have tried editing this code on my own very unsuccessfully to accomplish what I need. Anything anyone with more expertise than myself can do ?
 
Upvote 0
After several tries I still have been unable to make this code import the data into an existing workbook. Any help is very appreciated.
 
Upvote 0
This is not tested, but it may be a start for you.
It will copy all sheets from all Excel files it the directory you designate to your destination file.

Code:
Sub copyallfiles()

Dim i As Integer
Dim DestWB, DataWBname, Nfilename  As String


directory = "C:\directoryname\"  'change directory name and drive letter to needed directory

Workbooks.Open filename:="c:\directoryname\Sr Mgr Metrics template.xls" 'change directory name and drive letter to needed directory
DestWB = "Sr Mgr Metrics template"
'
 
 f = Dir(directory, 7)  'sets f equal to the first file name
    Do While f <> ""        'loops until there are no more files in the direstory
    
    If Right(f, 3) = "xls" And f <> "Sr Mgr Metrics template.xls" Then   ' check that it is an Excel file
 
         Nfilename = directory & f
         DataWBname = Left(f, Len(f) - 4)
         
         Workbooks.Open filename:=Nfilename  'Opens the Excel file, name has path in ti
       For i = 1 To Workbooks(DataWBname).Sheets.Count  'itterates through all sheets
        Sheets(i).Copy After:=Workbooks(DestWB).Sheets(Workbooks(DestWB).Sheets.Count) 'copies sheet to end of destination file
       Next i
       Workbooks(Nfilename).Close False   'Closes data file
       
    End If

     f = Dir            'set f equal to the next file name in the directory
    Loop

End Sub

I hope this helps
If you just need those 6 data files copied then you can just open each one and do the copy part of the code.
 
Upvote 0
Thanks for your attempt Mooseman. I am not very good with VBA at all but this code you have just seems to open one of the workbooks in question. I can't it to copy into the destination
 
Upvote 0
Would it make things less complicated if I just needed the first sheet of the 6 source workbooks

The source workbooks are

Manager Rita.xls
Manager Amy.xls
Manager Steve.xls
Manager Patricia.xls
Manager Mike.xls
Manager Jan.xls

and had them copied into the sheet in the sr mgr template called "data download" ? Just trying to simplify things for those nice enough to help
 
Upvote 0
Try to just step through this macro (F8), this way you can see each step as it is being completed.


Code:
Sub copyallfiles()

Dim i As Integer
Dim DestWB, DataWBname, Nfilename  As String


directory = "O:\Customer Service\Team Metrics" \ "  'change directory name and drive letter to needed directory"

Workbooks.Open filename:=directory & "Sr Mgr Metrics template.xls" 
DestWB = "Sr Mgr Metrics template"
DataWBname = Left(f, Len(f) - 4)
 
 
         Nfilename = directory & "Manager Rita.xls"
               
         Workbooks.Open filename:=Nfilename  'Opens the Excel data file
                  Sheets(1).Copy After:=Workbooks(DestWB).Sheets(Workbooks(DestWB).Sheets.Count) 'copies sheet to end of destination file
         Workbooks(Nfilename).Close False   'Closes data file
         
         Nfilename = directory & "Manager Amy.xls"
               
         Workbooks.Open filename:=Nfilename  'Opens the Excel data file
                  Sheets(1).Copy After:=Workbooks(DestWB).Sheets(Workbooks(DestWB).Sheets.Count) 'copies sheet to end of destination file
         Workbooks(Nfilename).Close False   'Closes data file
         
         Nfilename = directory & "Manager Steve.xls"
               
         Workbooks.Open filename:=Nfilename  'Opens the Excel data file
                  Sheets(1).Copy After:=Workbooks(DestWB).Sheets(Workbooks(DestWB).Sheets.Count) 'copies sheet to end of destination file
         Workbooks(Nfilename).Close False   'Closes data file
         
         Nfilename = directory & "Manager Patricia.xls"
               
         Workbooks.Open filename:=Nfilename  'Opens the Excel data file
                  Sheets(1).Copy After:=Workbooks(DestWB).Sheets(Workbooks(DestWB).Sheets.Count) 'copies sheet to end of destination file
         Workbooks(Nfilename).Close False   'Closes data file
         
         Nfilename = directory & "Manager Mike.xls"
               
         Workbooks.Open filename:=Nfilename  'Opens the Excel data file
                  Sheets(1).Copy After:=Workbooks(DestWB).Sheets(Workbooks(DestWB).Sheets.Count) 'copies sheet to end of destination file
         Workbooks(Nfilename).Close False   'Closes data file
         
         Nfilename = directory & "Manager Jan.xls"
               
         Workbooks.Open filename:=Nfilename  'Opens the Excel data file
                  Sheets(1).Copy After:=Workbooks(DestWB).Sheets(Workbooks(DestWB).Sheets.Count) 'copies sheet to end of destination file
         Workbooks(Nfilename).Close False   'Closes data file
       
   Workbooks(DataWBname).Close
End Sub
 
Upvote 0
Code:
Sub CombineFilesextra()
'========================================================================
' THIS COMBINES ALL SHEETS FROM ALL WORKBOOKS IN A DIRECTORY INTO ONE WORKBOOK
' THEN COMBINES ALL ONTO ONE SHEET
' PUT CODE IN THIS WORKBOOK (OR PERSONAL) - ******NOT MODULE*****
'========================================================================
    Dim Path As String
    Dim Filename As String
    Dim Wkb As Workbook
    Dim ws As Worksheet
    On Error Resume Next
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    '###################################
    Path = "C:\Documents and Settings\All Users\Documents\My Documents\Work Stuff\Test Folder"    'Change as needed  #
    '###################################
    Filename = Dir(Path & "\*.xls", vbNormal)
    Do Until Filename = ""
        Set Wkb = Workbooks.Open(Filename:=Path & "\" & Filename)
        For Each ws In Wkb.Worksheets
            ws.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
        Next ws
        Wkb.Close False
        Filename = Dir()
    Loop
 
    ' ALL SHEETS COMBINED - CODE BELOW
    Sheets.Add Before:=Sheets(1)
    Sheets(1).Activate
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> ActiveSheet.Name Then
            ws.UsedRange.Offset(0).Copy
            With Range("A65536").End(xlUp).Offset(2, 0)
    ' Change Offset to number of rows blank between 2 = 1 blank row, 3 = 2 Blank rows
                .PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
                              False, Transpose:=False
                .PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
                              False, Transpose:=False
            End With
        End If
    Next
    Sheets(1).Select
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

Hi

Try the code above

Should combine all of the sheets in all of the workbooks, them summarise all of those sheets onto one

Don't know whether it is exactly what you need, but maybe worth a look

Mark:)
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,404
Members
449,156
Latest member
LSchleppi

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