+ Reply to Thread
Results 1 to 14 of 14

Consolidate multiple worksheets into one

Hybrid View

  1. #1
    Registered User
    Join Date
    10-16-2013
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 2007
    Posts
    8

    Consolidate multiple worksheets into one

    Hi gurus,
    I am sure this has been done before and I just can't find it. I have a workbook that has several sheets in it with various rows of data on each sheet. I would like to have a macro to loop through all the sheets in the workbook and copy and paste the rows into a new summary sheet. The rows to be copied should only have data in Column A, in other words if Column A of a row is blank I want it skipped. Also Row 1 of every sheet contains my headers, and I am only using columns A through M. It needs to be able to copy rows even when autofilter is in use.

    Thanks,
    Rachael

  2. #2
    Registered User
    Join Date
    08-19-2014
    Location
    Pittsburgh, Pennsylvania
    MS-Off Ver
    2010
    Posts
    45

    Re: Consolidate multiple worksheets into one

    Like this?

    Sub MergeAll()
    Dim ws As Worksheet
    Dim LR As Long
    Dim shMerge As Worksheet
    
    Set shMerge = ActiveSheet
    
    For Each ws In ActiveWorkbook.Worksheets
    LR = shMerge.Cells(Rows.Count, "A").End(xlUp).Row
    
    If ws.Name <> shMerge.Name Then
    ws.UsedRange.Offset(1, 0).Copy
    shMerge.Range("A" & LR).Offset(1, 0).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End If
    Next
    
    LR = shMerge.Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = 2 To LR
    If shMerge.Range("A" & i).Value = "" Then
    shMerge.Rows(i).EntireRow.Delete
    End If
    Next i
    
    End Sub

  3. #3
    Registered User
    Join Date
    10-16-2013
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Consolidate multiple worksheets into one

    Sorry Jaime that didn't work, it still copies some of the rows where column A is empty. Would it matter if my data is arranged as tables?

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Consolidate multiple worksheets into one

    Maybe:

    Sub raeray38501()
    'YOU NEED TO CREATE A SHEET CALLED Summary FIRST
    Dim ws As Worksheet
    Dim i As Long
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Summary" Then
    For i = 2 To ws.Range("A" & Rows.count).End(3).Row
        If ws.Range("A" & i) <> "" Then
            ws.Range(Cells(i, "A"), Cells(i, "M")).Copy Sheets("Summary").Range("A" & Rows.count).End(3)(2)
        End If
    Next i
    Next ws
    End Sub
    Last edited by JOHN H. DAVIS; 08-20-2014 at 10:27 AM.

  5. #5
    Registered User
    Join Date
    10-16-2013
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Consolidate multiple worksheets into one

    John,
    I get a compile error on yours. Looks like it is missing an "End If" statement but when I add that it says "For without Next"

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Consolidate multiple worksheets into one

    Quote Originally Posted by raeray38501 View Post
    John,
    I get a compile error on yours. Looks like it is missing an "End If" statement but when I add that it says "For without Next"
    I caught the error and saved it. You probably looked at it before the changes.

  7. #7
    Registered User
    Join Date
    12-25-2013
    Location
    england
    MS-Off Ver
    2010
    Posts
    29

    Re: Consolidate multiple worksheets into one

    hi how would this code be changed to copy more than one column say column A through to column M and rows 1 to say 200.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Consolidate multiple worksheets into one

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Everyone who confuses correlation and causation ends up dead.

  9. #9
    Registered User
    Join Date
    08-19-2014
    Location
    Pittsburgh, Pennsylvania
    MS-Off Ver
    2010
    Posts
    45

    Re: Consolidate multiple worksheets into one

    No, now that I look at this, I believe this loop will not function properly:
    For i = 2 To LR
    If shMerge.Range("A" & i).Value = "" Then
    shMerge.Rows(i).EntireRow.Delete
    End If
    Next i
    Once a row is deleted, the next row that the loop should hit would be the same as the prior row number.
    The way the loop works, if Row 2 had a blank cell in that column, that row would be deleted, and the entire table would shift up one row.
    The next row that the loop should hit should be Row 2 again to check the next item, make sense?

    I'll modify today when time allows.

  10. #10
    Registered User
    Join Date
    10-16-2013
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Consolidate multiple worksheets into one

    I found Ron de Bruin's code to merge, which works pretty well except I would like to modify it only take the rows that have values in column A.

    Sub CopyDataWithoutHeaders()
        Dim sh As Worksheet
        Dim DestSh As Worksheet
        Dim Last As Long
        Dim shLast As Long
        Dim CopyRng As Range
        Dim StartRow As Long
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'Delete the sheet "RDBMergeSheet" if it exist
        Application.DisplayAlerts = False
        On Error Resume Next
        ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
    
        'Add a worksheet with the name "RDBMergeSheet"
        Set DestSh = ActiveWorkbook.Worksheets.Add
        DestSh.Name = "RDBMergeSheet"
    
        'Fill in the start row
        StartRow = 2
    
        'loop through all worksheets and copy the data to the DestSh
        For Each sh In ActiveWorkbook.Worksheets
            If sh.Name <> DestSh.Name Then
    
                'Copy header row, change the range if you use more columns
                If WorksheetFunction.CountA(DestSh.UsedRange) = 0 Then
                    sh.Range("A1:M1").Copy DestSh.Range("A1")
                End If
                
                'Find the last row with data on the DestSh and sh
                Last = LastRow(DestSh)
                shLast = LastRow(sh)
    
                'If sh is not empty and if the last row >= StartRow copy the CopyRng
                If shLast > 0 And shLast >= StartRow Then
    
                    'Set the range that you want to copy
                    Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
    
                    'Test if there enough rows in the DestSh to copy all the data
                    If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
                        MsgBox "There are not enough rows in the Destsh"
                        GoTo ExitTheSub
                    End If
    
                    
                    CopyRng.Copy
                    With DestSh.Cells(Last + 1, "A")
                        .PasteSpecial xlPasteValues
                        Application.CutCopyMode = False
                    End With
    
                End If
    
            End If
        Next
    
    ExitTheSub:
    
        Application.Goto DestSh.Cells(1)
    
        'AutoFit the column width in the DestSh sheet
        DestSh.Columns.AutoFit
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Consolidate all data in multiple worksheets of multiple workbooks in one Master file.
    By adil.master in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2014, 09:59 PM
  2. consolidate multiple worksheets
    By Mbutler132000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-26-2010, 06:10 AM
  3. Consolidate Multiple Worksheets
    By DGA2008 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2009, 09:03 AM
  4. Consolidate Multiple Worksheets Into One
    By DGA2008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-29-2009, 05:49 PM
  5. How to consolidate data from multiple worksheets.
    By SAR in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 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