Consolidate excel worksheet data into one sheet based on a list

ea123

New Member
Joined
Dec 13, 2008
Messages
21
Hello, I have this code below that copies data from all worksheets and copies it into one consolidated tab. My question is: is it possible to maintain a list on another tab called "List" and only select those worksheets that are part of that List to copy and consolidate data? Let us say I have 5 worksheets but I only want to copy and consolidate data from 2 of those called List 1 and List2 which are values maintained in tab called List? Thanks much!

Sub Consolidate()


Dim i As Integer
Dim w As Worksheet


On Error Resume Next
Sheets(1).Select
Worksheets.Add ' add a worksheet for consolidation of other tabs
Sheets(1).Name = "Consolidate"


' copy headings
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")


For Each w In ActiveWorkbook.Sheets
If w.Name <> "Consolidate" Then
Application.GoTo Sheets(w.Name).[a1]
Selection.CurrentRegion.Select
' Don't copy the headings
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets("Consolidate"). _
Cells(Rows.Count, 1).End(xlUp)(2)
End If
Next
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Largely untested, but this should do the job:

Code:
Option Explicit
Sub Consolidate()

    'Dim i As Integer
    Dim w As Worksheet
    Dim rngMyCell As Range
    Dim lngLastRow As Long
    
    On Error Resume Next
        Sheets(1).Select
        Worksheets.Add ' add a worksheet for consolidation of other tabs
        Sheets(1).Name = "Consolidate"
        
        ' copy headings
        Sheets(2).Activate
        Range("A1").EntireRow.Select
        Selection.Copy Destination:=Sheets(1).Range("A1")
        
        'Works from cell A2 down column A from the 'List' tab.  Change to suit if necessary.
        lngLastRow = Sheets("List").Cells(Rows.Count, "A").End(xlUp).Row
        For Each rngMyCell In Sheets("List").Range("A2:A" & lngLastRow)
            For Each w In ActiveWorkbook.Sheets
                If w.Name <> "Consolidate" Then
                    If w.Name = CStr(rngMyCell) Then
                        Application.Goto Sheets(w.Name).[a1]
                        Selection.CurrentRegion.Select
                        ' Don't copy the headings
                        Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
                        Selection.Copy Destination:=Sheets("Consolidate"). _
                        Cells(Rows.Count, 1).End(xlUp)(2)
                    End If
                End If
            Next w
        Next rngMyCell
    On Error GoTo 0
    
End Sub

Regards,

Robert
 
Upvote 0
Thanks, Robert. This is getting very close. But it copies the first item in the list to the first row in consolidate and then copies the rest of the worksheets as per the list... any idea why that might be? Thanks!


Largely untested, but this should do the job:

Code:
Option Explicit
Sub Consolidate()

    'Dim i As Integer
    Dim w As Worksheet
    Dim rngMyCell As Range
    Dim lngLastRow As Long
    
    On Error Resume Next
        Sheets(1).Select
        Worksheets.Add ' add a worksheet for consolidation of other tabs
        Sheets(1).Name = "Consolidate"
        
        ' copy headings
        Sheets(2).Activate
        Range("A1").EntireRow.Select
        Selection.Copy Destination:=Sheets(1).Range("A1")
        
        'Works from cell A2 down column A from the 'List' tab.  Change to suit if necessary.
        lngLastRow = Sheets("List").Cells(Rows.Count, "A").End(xlUp).Row
        For Each rngMyCell In Sheets("List").Range("A2:A" & lngLastRow)
            For Each w In ActiveWorkbook.Sheets
                If w.Name <> "Consolidate" Then
                    If w.Name = CStr(rngMyCell) Then
                        Application.Goto Sheets(w.Name).[a1]
                        Selection.CurrentRegion.Select
                        ' Don't copy the headings
                        Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
                        Selection.Copy Destination:=Sheets("Consolidate"). _
                        Cells(Rows.Count, 1).End(xlUp)(2)
                    End If
                End If
            Next w
        Next rngMyCell
    On Error GoTo 0
    
End Sub

Regards,

Robert
 
Upvote 0
I figured that out. Thanks. That solved my initial issue. Is it possible for this to open another workbook and copy the tabs from there instead of currently open workbook? Many thanks!
 
Upvote 0
Yes it's quite possible to get the data from another workbook (there are plenty of examples on the net) but if you can't find a solution it's best to start a new thread as this is different from your original request which has now been addressed.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top