Consolidate sheets

Watersource

New Member
Joined
Jan 3, 2009
Messages
17
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello all,

I am trying to consolidate a number of named sheets (005, 007 and 030) into a report sheet. All sheets have the same columns, only number of rows is different. I have managed to pull it off for one sheet, but all my tries to get it working for multiple sheets have failed. Row 9 is a header row, all data can be found starting row 10. Hope you can give me some tips on this.
VBA Code:
Sub CreateReport()

Dim lastrow As Long
Dim MySht, Mysheets
Mysheets = ("005", "007", "030")


Sheets("Report").Select
Range("A10").Select
Sheets("005").Select
Range("A10").Select
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Rows("10:" & lastrow).Select
Selection.Copy
Sheets("Report").Select
Range("A10").Select
ActiveSheet.Paste
Selection.End(xlDown).Select
ActiveCell.Offset(1).Select


End Sub

Thanks,
René
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
How about
VBA Code:
Sub Watersource()
   Dim Ws As Worksheet
   Dim NxtRw As Long
   
   With Sheets("Report")
      NxtRw = 10
      For Each Ws In Sheets(Array("005", "007", "030"))
         Ws.Range("A10", Ws.Range("A" & Rows.count).End(xlUp)).EntireRow.Copy .Range("A" & NxtRw)
         NxtRw = .Range("A" & Rows.count).End(xlUp).Offset(1).Row
      Next rw
   End With
End Sub
 
Upvote 0
Sub Watersource() Dim Ws As Worksheet Dim NxtRw As Long With Sheets("Report") NxtRw = 10 For Each Ws In Sheets(Array("005", "007", "030")) Ws.Range("A10", Ws.Range("A" & Rows.count).End(xlUp)).EntireRow.Copy .Range("A" & NxtRw) NxtRw = .Range("A" & Rows.count).End(xlUp).Offset(1).Row Next rw End With End Sub
Hello Fluff,

This indeed does the trick. I knew it had to be something like this but was not able to find the right syntax. There was just a (very) small typing error, instead Next Rw it is Next Ws.

Thanks so much for your help.

With kind regards,
René
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
Hello Fluff,

It shows I was a bit to quick :). When the sheets contain data, all goes well, but when e.g. sheet 005 only contains the header row(row 9), this line 9 is copied to the consolidated sheet. Would you have a trick to prevent this?

Thanks for your help on this.

With kind regards,
René
 
Upvote 0
How about
VBA Code:
Sub Watersource()
   Dim ws As Worksheet
   Dim NxtRw As Long, UsdRws As Long
   
   With Sheets("Report")
      NxtRw = 10
      For Each ws In Sheets(Array("005", "007", "030"))
         UsdRws = ws.Range("A" & Rows.count).End(xlUp).Row
         If UsdRws > 9 Then
            ws.Range("A10:A" & UsdRws).EntireRow.Copy .Range("A" & NxtRw)
            NxtRw = .Range("A" & Rows.count).End(xlUp).Offset(1).Row
         End If
      Next rw
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub Watersource()
   Dim ws As Worksheet
   Dim NxtRw As Long, UsdRws As Long
  
   With Sheets("Report")
      NxtRw = 10
      For Each ws In Sheets(Array("005", "007", "030"))
         UsdRws = ws.Range("A" & Rows.count).End(xlUp).Row
         If UsdRws > 9 Then
            ws.Range("A10:A" & UsdRws).EntireRow.Copy .Range("A" & NxtRw)
            NxtRw = .Range("A" & Rows.count).End(xlUp).Offset(1).Row
         End If
      Next rw
   End With
End Sub
Hi Fluff,

This indeed works :). Do I understand the code correctly that when UsdRws is larger then 9 this means there is something in line 10 and only then the lines are copied, and if this is not the case it goes to the next sheet?

Thanks for your help.

with kind regards,
René
 
Upvote 0
You understand correctly :)
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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