+ Reply to Thread
Results 1 to 10 of 10

Consolidation several tabs' data into a consolidation sheet via a loop?

Hybrid View

Gti182 Consolidation several tabs'... 07-30-2015, 06:51 AM
kadeo Re: Consolidation several... 07-30-2015, 07:20 AM
Gti182 Re: Consolidation several... 07-30-2015, 07:27 AM
kadeo Re: Consolidation several... 07-30-2015, 08:01 AM
JOHN H. DAVIS Re: Consolidation several... 07-30-2015, 07:32 AM
Gti182 Re: Consolidation several... 07-30-2015, 07:43 AM
JOHN H. DAVIS Re: Consolidation several... 07-30-2015, 07:56 AM
Gti182 Re: Consolidation several... 07-30-2015, 08:13 AM
kadeo Re: Consolidation several... 07-30-2015, 08:18 AM
JOHN H. DAVIS Re: Consolidation several... 07-30-2015, 08:16 AM
  1. #1
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Consolidation several tabs' data into a consolidation sheet via a loop?

    Hi all,

    I was hoping someone could help me with some loop code for below consolidation macro.

    My workbook has a "Consolidation" tab and several country tabs (all same format). My code consolidates all of the individual country tabs (after a filter is applied), one below the other in the consolidation tab.

    Below code works for 2 countries but there will be 20+ country tabs.

    I'm hoping someone can tweak my code so it can loop through all the country tabs rather than having to duplicate the code over and over like below.


    Sub Consolidate()
          
    Set CONS = Sheets("Consolidation")
    Set DEN = Sheets("Denmark")
    Set SPA = Sheets("Spain")
                
    
    'Denmark
    LR = DEN.Range("B" & Rows.Count).End(xlUp).Row
    DEN.Range("B9:CQ" & LR).AutoFilter Field:=94, Criteria1:="Data"
    
    LR = DEN.Range("B" & Rows.Count).End(xlUp).Row
    DEN.Range("B10:CQ" & LR).Copy
    CONS.Range("A2").PasteSpecial xlPasteValues
    
    'Spain
    LR = SPA.Range("B" & Rows.Count).End(xlUp).Row
    SPA.Range("B9:CQ" & LR).AutoFilter Field:=94, Criteria1:="Data"
    
    LR = SPA.Range("B" & Rows.Count).End(xlUp).Row
    SPA.Range("B10:CQ" & LR).Copy
    LR = CONS.Range("B" & Rows.Count).End(xlUp).Row
    CONS.Range("A" & LR + 1).PasteSpecial xlPasteValues
         
    End Sub
    Last edited by Gti182; 07-30-2015 at 08:13 AM.

  2. #2
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    How does this work for you?

    EDIT: Added a "Select Case" statement so you can add the names of the tabs you want to copy in case there are more tabs than just country tabs. This also makes sure the "Consolidation" tab is not included (if your name's not down, you're not coming in!)

    Sub Consolidate()
         
        Dim wSheet As Worksheet
        Dim Cons As Worksheet
        Dim PasteRow As Long
        Dim LR As Long
         
        Set Cons = Sheets("Consolidation")
                    
        For Each wSheet In ThisWorkbook.Sheets
            Select Case wSheet.Name
                Case "Denmark", "Spain", "Another country", "Yet another country"
                    LR = wSheet.Range("B" & Rows.Count).End(xlUp).Row
                    wSheet.Range("B9:CQ" & LR).AutoFilter Field:=94, Criteria1:="Data"
                    wSheet.Range("B10:CQ" & LR).Copy
                    PasteRow = Cons.Range("a" & Cons.Rows.Count).End(xlUp).Row + 1
                    Cons.Range("A" & PasteRow).PasteSpecial xlPasteValues
            End Select
        Next
         
    End Sub
    Last edited by kadeo; 07-30-2015 at 07:24 AM.
    Please click *Add Reputation if I've helped

  3. #3
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    wow that looks neat!

    getting a runtime error 1004 Autofilter method of Range class failed on below part of code?
    DEN.Range("B9:CQ" & LR).AutoFilter Field:=94, Criteria1:="Data"

  4. #4
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    I seem to remember something about having to activate a sheet before the filter works properly when looping through sheets. Just insert
    wSheet.Activate
    into the line above where LR is calculated, within the case in my code see if that works.

    Also, the code you pasted back is still working on DEN instead of wSheet.
    Last edited by kadeo; 07-30-2015 at 08:07 AM.

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

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    Maybe:

    Sub Gti182()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Consolidation" Then
            With ws
                    lr = .Range("B" & Rows.Count).End(xlUp).row
                    .Range("B9:CQ" & lr).AutoFilter 94, "Data"
                    .Range("B10:CQ" & lr).Copy Sheets("Consolidation").Range("A" & Rows.Count).End(3)(2)
            End With
        End If
    Next ws
    End Sub

  6. #6
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    Hi John that works a treat! Any way to just paste special values?

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

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    Did you only the visible cells in the range? Try:

    Sub Gti182()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Consolidation" Then
            With ws
                    lr = .Range("B" & Rows.Count).End(xlUp).row
                    .Range("B9:CQ" & lr).AutoFilter 94, "Data"
                    .Range("B10:CQ" & lr).SpecialCells(12).Copy
                    Sheets("Consolidation").Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
            End With
        End If
    Next ws
    End Sub

  8. #8
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    thanks kadeo and John both work great!

  9. #9
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    Quote Originally Posted by Gti182 View Post
    thanks kadeo and John both work great!
    A pleasure!

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

    Re: Consolidation several tabs' data into a consolidation sheet via a loop?

    You're welcome. Glad to help out and thanks for the feedback.

+ 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] Help for enhance the code - Excluding Sheet While Consolidation
    By ExcelUser2707 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-25-2014, 05:43 AM
  2. Copy data from header to header in consolidation sheet
    By Ignesh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-12-2013, 04:10 AM
  3. Work Sheet consolidation Macro
    By jwbeaty in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-20-2011, 04:05 AM
  4. Consolidation of workbooks in the same file to a single sheet in a master
    By Jaspabl in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2011, 12:11 AM
  5. sheet 1 needs to be daily consolidation of others
    By m3site in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-17-2011, 11:34 AM
  6. Buggy Sheet Consolidation Behaviour
    By shockeroo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2010, 07:42 AM
  7. Multiple consolidation sheet
    By Navin in forum Excel General
    Replies: 0
    Last Post: 03-15-2006, 11:10 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