+ Reply to Thread
Results 1 to 9 of 9

Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire)

Hybrid View

  1. #1
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire)

    Hi.
    I'm trying to adapt co code below.
    how to get it just the tabs that desire and not all that are in the spreadsheet?

    Erro in this line
    Set cs = ActiveWorkbook.Sheets(Array("Plan1", "Plan2", "Plan3", "Plan4", "Plan5")) '<-- Runtime Error 13 (incompatible types)
    Option Explicit
    
    Sub ConsolidarPlanilhas()
    'Author:    Jerry Beaucaire
    'Date:      6/26/2009
    'Updated:  6/23/2010
    'Merge all sheets in a workbook into one summary sheet (stacked)
    'Data is sorted by a specific column name
    Dim cs As Worksheet, ws As Worksheet
    Dim LR As Long, NR As Long, sCol As Long
    Dim sName As Boolean, SortStr As String
    Application.ScreenUpdating = False
    
    'From the headers in data sheets, enter the column title to sort by when finished
    SortStr = "Nome"
    
    'Add consolidation sheet if needed
    If Not Evaluate("ISREF(PlanFinal!A1)") Then _
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "PlanFinal"
    
    'Option to add sheet names to consolidation report
    sName = MsgBox("Adicione o nome da folha de relatório de PlanFinal?", vbYesNo + vbQuestion) = vbYes
    
    'Setup
    Set cs = ActiveWorkbook.Sheets(Array("Plan1", "Plan2", "Plan3", "Plan4", "Plan5")) '<-- Runtime Error 13 (incompatible types)
    cs.Cells.ClearContents
    NR = 1
    
    'Process each data sheet
        For Each ws In Worksheets
            If ws.Name <> cs.Name Then
                LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
                'customize this section to copy what you need
                If NR = 1 Then
                  'copy titles and data to start the consolidation, edit row as needed for source of titles
                    ws.Range("A1", ws.Cells(1, Columns.Count).End(xlToLeft)).Copy
                    If sName Then
                        cs.Range("B1").PasteSpecial xlPasteAll
                    Else
                        cs.Range("A1").PasteSpecial xlPasteAll
                    End If
                    NR = 2
                End If
                
                ws.Range("A2:BB" & LR).Copy    'copy data, edit as needed for the start row
    
                If sName Then      'paste and add sheet names if required
                    cs.Range("B" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                    cs.Range("A" & NR, cs.Range("B" & cs.Rows.Count).End(xlUp).Offset(0, -1)) = ws.Name
                Else
                    cs.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                End If
                
                NR = cs.Range("A" & cs.Rows.Count).End(xlUp).Row + 1
            End If
        Next ws
    
    'Sort
        LR = cs.Range("A" & cs.Rows.Count).End(xlUp).Row
        On Error Resume Next
        sCol = cs.Cells.Find(SortStr, After:=cs.Range("A1"), LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Column
        cs.Range("A1:BB" & LR).Sort Key1:=cs.Cells(2, sCol + (IIf(sName, 1, 0))), Order1:=xlAscending, _
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
    'Cleanup
        If sName Then cs.[A1] = "PlanFinal"
        cs.Rows(1).Font.Bold = True
        cs.Cells.Columns.AutoFit
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        cs.Activate
        Range("A1").Select
        Set cs = Nothing
    End Sub
    "No xadrez nem sempre a menor dist?ncia entre dois pontos ? uma linha reta" G. Kasparov.

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select b from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    You may not do that because cs is a Worksheet variable. You may use:
    Set cs = ActiveWorkbook.Sheets("PlanFinal")
    cs.Cells.ClearContents
    NR = 1
    
    'Process each data sheet
        For Each ws In ActiveWorkbook.Sheets(Array("Plan1", "Plan2", "Plan3", "Plan4", "Plan5"))

  3. #3
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    Sorry.
    your code returned the names of the tabs, should return the data

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    My code did not return anything - it only changed the loop.

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

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    Option Explicit
    
    Sub ConsolidarPlanilhas()
    'Author:    Jerry Beaucaire
    'Date:      6/26/2009
    'Updated:  6/23/2010
    'Merge all sheets in a workbook into one summary sheet (stacked)
    'Data is sorted by a specific column name
    Dim cs As Worksheet, ws As Worksheet
    Dim LR As Long, NR As Long, sCol As Long
    Dim sName As Boolean, SortStr As String
    Application.ScreenUpdating = False
    
    'From the headers in data sheets, enter the column title to sort by when finished
    SortStr = "Nome"
    
    'Add consolidation sheet if needed
    If Not Evaluate("ISREF(PlanFinal!A1)") Then _
        Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "PlanFinal"
    
    'Option to add sheet names to consolidation report
    sName = MsgBox("Adicione o nome da folha de relatório de PlanFinal?", vbYesNo + vbQuestion) = vbYes
    
    'Setup
    Set cs = Sheets("PlanFinal")
    cs.Cells.ClearContents
    NR = 1
    
    'Process each data sheet
          For Each ws In Sheets(Array("Plan1", "Plan2", "Plan3", "Plan4", "Plan5"))
        
                LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
                'customize this section to copy what you need
                If NR = 1 Then
                  'copy titles and data to start the consolidation, edit row as needed for source of titles
                    ws.Range("A1", ws.Cells(1, Columns.Count).End(xlToLeft)).Copy
                    If sName Then
                        cs.Range("B1").PasteSpecial xlPasteAll
                    Else
                        cs.Range("A1").PasteSpecial xlPasteAll
                    End If
                    NR = 2
                End If
                
                ws.Range("A2:BB" & LR).Copy    'copy data, edit as needed for the start row
    
                If sName Then      'paste and add sheet names if required
                    cs.Range("B" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                    cs.Range("A" & NR, cs.Range("B" & cs.Rows.Count).End(xlUp).Offset(0, -1)) = ws.Name
                Else
                    cs.Range("A" & NR).PasteSpecial xlPasteValuesAndNumberFormats
                End If
                
                NR = cs.Range("A" & cs.Rows.Count).End(xlUp).Row + 1
          
        Next ws
    
    'Sort
        LR = cs.Range("A" & cs.Rows.Count).End(xlUp).Row
        On Error Resume Next
        sCol = cs.Cells.Find(SortStr, After:=cs.Range("A1"), LookIn:=xlValues, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Column
        cs.Range("A1:BB" & LR).Sort Key1:=cs.Cells(2, sCol + (IIf(sName, 1, 0))), Order1:=xlAscending, _
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
    'Cleanup
        If sName Then cs.[A1] = "PlanFinal"
        cs.Rows(1).Font.Bold = True
        cs.Cells.Columns.AutoFit
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
        cs.Activate
        Range("A1").Select
        Set cs = Nothing
    End Sub

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

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    If you want to loop through select sheets

    Change the following lines
    Remove this line and replace it with

    Set cs = Sheets("PlanFinal") '<-- Runtime Error 13 (incompatible types)
    And this line
    For Each ws In Worksheets
    with

    'Process each data sheet
        For Each ws In Sheets(Array("Plan1", "Plan2", "Plan3", "Plan4", "Plan5"))
          If ws.Name <> cs.Name Then
    'No need for this line and end if

  7. #7
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    After the code runs, which has data in column A (Name Guide), column B (data in column B) etc..

    this is not happening

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

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    Please run the code by steeping over using F8 and see which line is not working, or better attach a sample.

  9. #9
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: Merge specific sheets in a workbook into one summary sheet (by 'Author:Jerry Beaucaire

    Hi, i make tests and it work!

    Thank you!!

+ 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. Replies: 13
    Last Post: 08-14-2013, 01:04 PM
  2. [SOLVED] VB to Create Summary Sheet using all other sheets within the workbook.
    By MICowboy13 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-27-2012, 01:49 PM
  3. Replies: 0
    Last Post: 03-27-2012, 04:54 PM
  4. Replies: 2
    Last Post: 08-18-2009, 11:19 AM
  5. copying data from sheets in workbook to summary sheet in same
    By Nets in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-19-2009, 03:49 PM

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