+ Reply to Thread
Results 1 to 5 of 5

Merge multiple excels with multiple sheets in a master excel with multiple sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2012
    Location
    bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    2

    Question Merge multiple excels with multiple sheets in a master excel with multiple sheets

    Hello!

    I've looked for several days and I didn't find an answer, so I need your help. I have 5 workbooks and each workbook has 10 sheet , I whant to collect all the information in a workbook named Master ( e.g. Workbook1 sheet A, Workbook 2 sheet A, Workbook 3 sheet A, Workbook 4 sheet A, Workbook 5 sheet A in a workbook master sheet A; Workbook1 sheet b, Workbook 2 sheet b, Workbook 3 sheet b, Workbook 4 sheet b, Workbook 5 sheet b in a workbook master sheet b from etc. b. I need to copy the data but not the head of the table. Data will be added each day.

    Is there any possibility of a macro to do this?

    I hope i has clear...If not please tell me...I don't write in English very well.

    Thank you in advance!

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Merge multiple excels with multiple sheets in a master excel with multiple sheets

    yes
    What if the master workbook already has values in lets say SheetA. Do you want to delete thais data before importing or appending it?
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  3. #3
    Registered User
    Join Date
    02-15-2012
    Location
    bucuresti
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Merge multiple excels with multiple sheets in a master excel with multiple sheets

    Appending data.

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Merge multiple excels with multiple sheets in a master excel with multiple sheets

    Here you Go:
    This one will delete the old data every time you run the macro
    I added some hints so you can know how it works
    Option Explicit
    Option Base 1
    
    Sub CollectWorkbooks()
    
    Dim varFiles As Variant
    varFiles = Application.GetOpenFilename("Excel-files,*.xls; *.xlsx; *.xlsm", _
        1, "Select Workbooks to Collect", , True)
    'checking if no files were selected
    On Error Resume Next
    If UBound(varFiles) = 0 Then
    End If
    If Err.Number = 13 Then Exit Sub
    On Error GoTo 0
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Dim n As Long, r As Long
    Dim f As Integer, s As Integer
    Dim wbSource As Workbook, wbMaster As Workbook
    Dim shSource As Worksheet, shDest As Worksheet
    Dim chkFIRST As Boolean
    
    Set wbMaster = ThisWorkbook
    'deleteing old sheets from the master file except the first one
    Do While wbMaster.Sheets.Count > 1
        wbMaster.ActiveSheet.Delete
    Loop
    'deleting records from the first sheet
    Set shDest = ThisWorkbook.Worksheets(1)
    r = shDest.Cells.SpecialCells(xlCellTypeLastCell).Row
    shDest.Range(1 & ":" & r).ClearContents
    
    'opening each file and importing the data
    chkFIRST = True
    For f = 1 To UBound(varFiles)
        Set wbSource = Workbooks.Open(varFiles(f), ReadOnly:=True)
        For Each shSource In wbSource.Worksheets
            
            'renaming the first sheet in the master file to the name of the first sheet in the first source file
            If chkFIRST = True Then
                shDest.Name = shSource.Name
                chkFIRST = False
            End If
            'getting the same sheet in the master file if it exists
            Set shDest = Nothing
            On Error Resume Next
            Set shDest = wbMaster.Worksheets(shSource.Name)
            r = shDest.Cells.SpecialCells(xlCellTypeLastCell).Row
            On Error GoTo 0
            'if the sheet doesnt exist then create it
            If shDest Is Nothing Then
                Err.Clear
                Set shDest = wbMaster.Worksheets.Add(after:=wbMaster.Worksheets(wbMaster.Worksheets.Count))
                shDest.Name = shSource.Name
                r = 1
            End If
            'adding the records
            r = r + 1
            n = shSource.Cells.SpecialCells(xlCellTypeLastCell).Row
            s = shSource.Cells.SpecialCells(xlCellTypeLastCell).Column
            shSource.Range(shSource.Cells(2, 1).Address & ":" & shSource.Cells(n, s).Address).Copy _
            Destination:=shDest.Range(shDest.Cells(r, 1).Address & ":" & shDest.Cells(r + n, s).Address)
        Next
        wbSource.Close
    Next
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    Last edited by Kelshaer; 02-15-2012 at 10:37 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Merge multiple excels with multiple sheets in a master excel with multiple sheets

    This one will append the new data to the existing

    Option Explicit
    Option Base 1
    
    Sub CollectWorkbooks()
    
    Dim varFiles As Variant
    varFiles = Application.GetOpenFilename("Excel-files,*.xls; *.xlsx; *.xlsm", _
        1, "Select Workbooks to Collect", , True)
    'exiting if no files were selected
    On Error Resume Next
    If UBound(varFiles) = 0 Then
    End If
    If Err.Number = 13 Then Exit Sub
    On Error GoTo 0
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim n As Long, r As Long
    Dim f As Integer, s As Integer
    
    Dim wbSource As Workbook, wbMaster As Workbook
    Dim shSource As Worksheet, shDest As Worksheet
    Set wbMaster = ThisWorkbook
    
    
    'opening each file and importing the data
    For f = 1 To UBound(varFiles)
        Set wbSource = Workbooks.Open(varFiles(f), ReadOnly:=True)
        For Each shSource In wbSource.Worksheets
            'getting the same sheet in the master file if it exists
            Set shDest = Nothing
            On Error Resume Next
            Set shDest = wbMaster.Worksheets(shSource.Name)
            r = shDest.Cells.SpecialCells(xlCellTypeLastCell).Row
            On Error GoTo 0
            'if the sheet doesnt exist then create it
            If shDest Is Nothing Then
                Err.Clear
                Set shDest = wbMaster.Worksheets.Add(after:=wbMaster.Worksheets(wbMaster.Worksheets.Count))
                shDest.Name = shSource.Name
                r = 1
            End If
            'adding the records
            r = r + 1
            n = shSource.Cells.SpecialCells(xlCellTypeLastCell).Row
            s = shSource.Cells.SpecialCells(xlCellTypeLastCell).Column
            shSource.Range(shSource.Cells(2, 1).Address & ":" & shSource.Cells(n, s).Address).Copy _
            Destination:=shDest.Range(shDest.Cells(r, 1).Address & ":" & shDest.Cells(r + n, s).Address)
            
        Next
        wbSource.Close
    
    Next
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1