Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50085

How to adapt the macro to allow it to consolidate files with multiple sheets?

$
0
0
The macro below is able to combine multiple Excel files in a folder into a single workbook (each sheet of the new workbook represents the original individual Excel file). The little drawback is that the original Excel files is ONLY allowed to have ONE sheet for this macro to work. If a file has multiple sheets, then only one sheet is used for the consolidation.

Is there a way to modify it to allow it to accommodate multiple sheets of files to be consolidated?


Code:


Option Explicit

Sub cons_data()

Dim Master As Workbook
Dim sourceBook As Workbook
Dim sourceData As Worksheet
Dim CurrentFileName As String
Dim myPath As String
Dim sname As String

Application.ScreenUpdating = False

myPath = "C:\2011\Files"                    'The folder containing the files to be combined

CurrentFileName = Dir(myPath & "\*.xls")    'Finds the name of the first file of type .xls in the current directory

Set Master = ThisWorkbook                  'Create a workbook for the recap report

Do
    Workbooks.Open (myPath & "\" & CurrentFileName)
    Set sourceBook = Workbooks(CurrentFileName)
    Set sourceData = sourceBook.Worksheets(1)
   
    With sourceData
        sname = Left(CurrentFileName, Len(CurrentFileName) - 4)
        Master.Worksheets.Add(after:=Master.Worksheets(Master.Worksheets.Count)).Name = sname
        .Cells.Copy Master.Worksheets(sname).Range("A1")
    End With
     
    sourceBook.Close
 
    'Calling DIR w/o argument finds the next xls file within the current directory.
    CurrentFileName = Dir()
Loop While CurrentFileName <> ""

Application.ScreenUpdating = True

End Sub


Viewing all articles
Browse latest Browse all 50085

Trending Articles