+ Reply to Thread
Results 1 to 13 of 13

Consolidate Multiple Sheets into Master

Hybrid View

  1. #1
    Registered User
    Join Date
    11-17-2012
    Location
    Alberta
    MS-Off Ver
    Excel 2007
    Posts
    19

    Consolidate Multiple Sheets into Master

    Ok, I have a code that is working great, except for two things. I'd like to have the information on the Summary sheet start on Row 6, and have the names of the worksheets the info came from in Column A.

    Code is as follows

    Sub CombineAllSheets()
        Dim wsCopyTo As Worksheet
        Dim lngLastRow As Long
        Dim lngLoopCtr As Long
        Dim ws As Worksheet
        Dim lngLastRow2 As Long
        Sheets(1).Select
        Application.ScreenUpdating = False
        On Error Resume Next
        With Worksheets("Summary")
            If Err Then Worksheets.Add
            ActiveSheet.Name = "Summary"
            On Error GoTo 0
            Set wsCopyTo = Sheets("Summary")
        End With
        For Each ws In Sheets
            If ws.Name <> "Summary" Then
                lngLastRow2 = ws.Cells(Rows.Count, "B").End(xlUp).Row
                lngLastRow = wsCopyTo.Cells(Rows.Count, "B").End(xlUp).Row
                ws.Range("A6:K" & lngLastRow2).Copy wsCopyTo.Range("B" & lngLastRow + 1)
            End If
    
    Next
        lngLastRow = wsCopyTo.Cells(Rows.Count, "B").End(xlUp).Row
        With wsCopyTo.Range("A6:K" & lngLastRow)
            .Columns.AutoFit
        End With
        For lngLoopCtr = lngLastRow To 2 Step -1
            If Cells(lngLoopCtr, "B") = "" Then
                Rows(lngLoopCtr).EntireRow.Delete
            End If
        Next lngLoopCtr
        Application.ScreenUpdating = True
    End Sub
    Also would like to have the Summary automatically update if I enter new data into the individual sheets, and to know, if I format the Summary sheet previously to running the macro, will it keep the formatting? (ex. hide columns etc)

    I hope this is enough information, and any help will be greatly appreciated.

    Thanks in advance!!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidate Multiple Sheets into Master

    This line is copying
    ws.Range("A6:K" & lngLastRow2).Copy wsCopyTo.Range("B" & lngLastRow + 1)
    in to first empty column "B" of summary sheet, which is probably "B1". If you wish the copy to start at Rows 6, the easiset option would be to fill in data(Suspect heading) in "B1-B5", so that the next empyt would become B6.

  3. #3
    Registered User
    Join Date
    11-17-2012
    Location
    Alberta
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Consolidate Multiple Sheets into Master

    Thank you, AB33!
    That's perfect

    Now I just need to figure out how to get the sheet names in column A, and I can probably clear all the data from "Summary" before re-running the macro to update, instead of having it go automatically.

    Thanks again!

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidate Multiple Sheets into Master

    Slightly improved code

    Sub CombineAllSheets()
        Dim ms As Worksheet, LRms As Long, lngLoopCtr As Long, ws As Worksheet, LR As Long
        Application.ScreenUpdating = False
        On Error Resume Next
            If Not Evaluate("ISREF(Summary!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Summary"
            Else
            Sheets("Summary").UsedRange.Offset(5, 0).ClearContents
            End If
            Set ms = Sheets("Summary")
        For Each ws In Sheets
            If ws.Name <> "Summary" Then
                LR = ws.Cells(Rows.Count, "B").End(xlUp).Row
                LRms = ms.Cells(Rows.Count, "B").End(xlUp).Row
                ws.Range("A6:K" & LR).Copy ms.Range("B" & LRms + 1)
                Rng = ws.Cells(Rows.Count, 1).End(xlUp).Row - 1
                ms.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(Rng) = ws.Name
                
            End If
    
    Next
        LRms = ms.Cells(Rows.Count, "B").End(xlUp).Row
        With ms.Range("A6:K" & LRms)
            .Columns.AutoFit
        End With
        For lngLoopCtr = LRms To 2 Step -1
            If Cells(lngLoopCtr, "B") = "" Then
                Rows(lngLoopCtr).EntireRow.Delete
            End If
        Next lngLoopCtr
        Application.ScreenUpdating = True
    End Sub

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidate Multiple Sheets into Master

    Yes, I am afraid. I had a discussion on this forum regarding automatic update. It is not shared by the most experienced users of VBA

    Sub CombineAllSheets()
        Dim ms As Worksheet, LRms As Long, lngLoopCtr As Long, ws As Worksheet, LR As Long
        Application.ScreenUpdating = False
        On Error Resume Next
        With Worksheets("Summary")
            If Err Then Worksheets.Add
            ActiveSheet.Name = "Summary"
            On Error GoTo 0
            Set ms = Sheets("Summary")
        End With
        For Each ws In Sheets
            If ws.Name <> "Summary" Then
                LR = ws.Cells(Rows.Count, "B").End(xlUp).Row
                LRms = ms.Cells(Rows.Count, "B").End(xlUp).Row
                ws.Range("A6:K" & LR).Copy ms.Range("B" & LRms + 1)
                Rng = ws.Cells(Rows.Count, 1).End(xlUp).Row - 1
                ms.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(Rng) = ws.Name
                  
            End If
    
    Next
        LRms = ms.Cells(Rows.Count, "B").End(xlUp).Row
        With ms.Range("A6:K" & LRms)
            .Columns.AutoFit
        End With
        For lngLoopCtr = LRms To 2 Step -1
            If Cells(lngLoopCtr, "B") = "" Then
                Rows(lngLoopCtr).EntireRow.Delete
            End If
        Next lngLoopCtr
        Application.ScreenUpdating = True
    End Sub

  6. #6
    Registered User
    Join Date
    11-17-2012
    Location
    Alberta
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Consolidate Multiple Sheets into Master

    Oh, you're a genius!

    Unfortunately it seems to be over-copying the sheet names, and is copying row 5 from some sheets. Would it be easier if I uploaded a copy of my WB for you to see what I'm trying to do?

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidate Multiple Sheets into Master

    Yes, post the whole book

  8. #8
    Registered User
    Join Date
    11-17-2012
    Location
    Alberta
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Consolidate Multiple Sheets into Master

    Ok, here goes. What I'm looking for is: the corresponding client name only beside their information, only ranges A6:K(end) <- could be only one line on some clients, or up to 50 (or more) on others, not the column headers, and not the "Template" sheet. I hope this makes sense. I'm really sorry it's been so complicated, but I really do appreciate all your help!
    Attached Files Attached Files
    Last edited by megmer; 11-17-2012 at 03:37 PM.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidate Multiple Sheets into Master

    Try this one
    Sub CombineAllSheets()
        Dim ms As Worksheet, LRms As Long, lngLoopCtr As Long, ws As Worksheet, LR As Long
        Application.ScreenUpdating = False
        On Error Resume Next
        If Not Evaluate("ISREF(Summary!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Summary"
        Else
            Set ms = Sheets("Summary")
        LR = ms.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If LR >= 6 Then
            Sheets("Summary").Range("A6:K" & LR).ClearContents
            End If
        End If
            
        For Each ws In Sheets
            If ws.Name <> "Summary" And ws.Name <> "Template" Then
                LR = ws.Range("A:K").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                LRms = ms.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                ws.Range("A6:K" & LR).Copy ms.Range("B" & ms.Range("B:K").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1)
                Rng = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 5
                ms.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(Rng) = ws.Name
                 
            End If
    Next
        
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    Last edited by AB33; 11-17-2012 at 04:24 PM.

  10. #10
    Registered User
    Join Date
    11-17-2012
    Location
    Alberta
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Consolidate Multiple Sheets into Master

    SO close! Now all I need is for it to not copy the 'empty' rows. I can't say enough how much I appreciate all your help. This would've taken me weeks to get this far :D

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidate Multiple Sheets into Master

    Okay, There was a code which deletes a row if column "B" is empty, I removed it because I was not sure its purpose, I have now put it back. If you wish to delete a row if columns G, or F are zeros, you can change this line of code from "B" to either G, or F.
    If Cells(i, "B") = "" Then"

    Sub CombineAllSheets()
     Dim ms As Worksheet, LRms As Long, ws As Worksheet, LR As Long, Rng As Long, i As Long
        Application.ScreenUpdating = 0
        Application.DisplayAlerts = 0
        On Error Resume Next
        If Not Evaluate("ISREF(Summary!A1)") Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Summary"
        Else
            Set ms = Sheets("Summary")
        LR = ms.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If LR >= 6 Then
            Sheets("Summary").Range("A6:K" & LR).ClearContents
            End If
        End If
            
        For Each ws In Sheets
            If ws.Name <> "Summary" And ws.Name <> "Template" Then
                LR = ws.Range("A:K").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                LRms = ms.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                ws.Range("A6:K" & LR).Copy ms.Range("B" & ms.Range("B:K").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1)
                Rng = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row - 5
                ms.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(Rng) = ws.Name
                 
            End If
        Next ws
            LRms = ms.Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            With ms.Range("A6:K" & LRms)
            .Columns.AutoFit
            End With
            For i = LRms To 6 Step -1
                If Cells(i, "B") = "" Then
                Rows(i).EntireRow.Delete
                End If
            Next i
         Application.ScreenUpdating = 1
        
         Application.DisplayAlerts = 1
    End Sub
    Attached Files Attached Files
    Last edited by AB33; 11-18-2012 at 07:38 AM.

  12. #12
    Registered User
    Join Date
    11-17-2012
    Location
    Alberta
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Consolidate Multiple Sheets into Master

    Thank you SO So much, that is perfect!

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidate Multiple Sheets into Master

    You are welcome!
    Could you please close this thread as closed?

+ 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