Results 1 to 4 of 4

Macro to combine multiple sheets from multiple workbooks

Threaded View

  1. #1
    Registered User
    Join Date
    06-21-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    8

    Macro to combine multiple sheets from multiple workbooks

    Hi There,
    I am looking for a macro that will open multiple workbooks and consolidate the worksheets on one tab. Each workbook will have the same number and named tabs (ie Sheet 1, Sheet 2) and the data will start in cell B5. In the end I am looking to consolidate all the Sheet 1's with a source range on one tab and all the Sheet 2's with a source range on another tab. I would also like only one header in the consolidated worksheet.

    I have attached a copy of what one workbook would look like.

    I have been using this code and it works okay if you have 1 tab, but it consolidates all rows when I want it to stop and the last cell with data in column B.

    Sub merge_Tracker_workbooks()
    
        Dim SummarySheet As Worksheet
        Dim FolderPath As String
        Dim SelectedFiles() As Variant
        Dim NRow As Long
        Dim FileName As String
        Dim NFile As Long
        Dim WorkBk As Workbook
        Dim SourceRange As Range
        Dim DestRange As Range
        
        ' Create a new workbook and set a variable to the first sheet.
        Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
        
        ' Modify this folder path to point to the files you want to use.
        FolderPath = "C:\My Data\1 Stacy\3 Projects\Q3\Field Huddle Boards"
        
        ' Set the current directory to the the folder path.
        ChDrive FolderPath
        ChDir FolderPath
        
        ' Open the file dialog box and filter on Excel files, allowing multiple files
        ' to be selected.
        SelectedFiles = Application.GetOpenFilename( _
            filefilter:="Excel Files (*.xlsx*), *.xlsx*", MultiSelect:=True)
        
        ' NRow keeps track of where to insert new rows in the destination workbook.
        NRow = 1
        
        ' Loop through the list of returned file names
        For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
            ' Set FileName to be the current workbook file name to open.
            FileName = SelectedFiles(NFile)
            
            ' Open the current workbook.
            Set WorkBk = Workbooks.Open(FileName)
            
            ' Set the cell in column A to be the file name.
            SummarySheet.Range("A" & NRow).Value = FileName
            
            ' Set the source range to be A9 through C9.
            ' Modify this range for your workbooks. It can span multiple rows.
        Dim LastRow As Long
        LastRow = WorkBk.Worksheets(1).Cells.Find(What:="*", _
                     After:=WorkBk.Worksheets(1).Cells.Range("a1"), _
                     SearchDirection:=xlPrevious, _
                     LookIn:=xlFormulas, _
                     SearchOrder:=xlByRows).Row
        Set SourceRange = WorkBk.Worksheets(1).Range("a3:r" & LastRow)
            
            ' Set the destination range to start at column a and be the same size as the source range.
            Set DestRange = SummarySheet.Range("a" & NRow)
            Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
               SourceRange.Columns.Count)
               
            ' Copy over the values from the source to the destination.
            DestRange.Value = SourceRange.Value
            
            ' Increase NRow so that we know where to copy data next.
            NRow = NRow + DestRange.Rows.Count
            
            ' Close the source workbook without saving changes.
            WorkBk.Close savechanges:=False
        Next NFile
        
        ' Call AutoFit on the destination sheet so that all data is readable.
        SummarySheet.Columns.AutoFit
    
    End Sub
    Attached Files Attached Files
    Last edited by jeffreybrown; 10-10-2018 at 02:10 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 02-06-2018, 12:53 PM
  2. Vba/macro to combine multiple workbooks with multiple sheets into one workbook
    By ReshayM in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-03-2015, 01:00 PM
  3. Excel 2007 - Combine Multiple Workbooks into one Workbook Multiple Sheets
    By mab in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-29-2014, 02:48 PM
  4. Combine several workbooks containing data in multiple sheets into a master Workbook
    By sunrize9 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2014, 09:10 PM
  5. Required Macro for combine multiple workbooks which contains multiple worksheets
    By DHANASEKARAN in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-06-2013, 03:20 PM
  6. Combine multiple sheets of multiple workbooks in one sheet
    By Djibril69 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2013, 05:18 PM
  7. [SOLVED] HELP TO COMBINE MULTIPLE SHEETS IN MULTIPLE WORKBOOKS
    By Eddy Stan in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-13-2006, 08:50 AM

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