Macro to Consolidate Data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have written code to consolidate data from sheet1 up to one sheet before the last sheet and to paste these in sheet "Consolidated Data"

The data is copied and pasted from the respective sheets

I need my macro amended so that the data is pasted from row 2 onwards on sheet "Consolidated Data"


It would be appreciated if someone could amend my code accordingly

Code:
 Sub Consolidated ()

Dim LR As Long

With Sheets("Consolidated Data")
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A2:M").ClearContents
 ShtCount = ActiveWorkbook.Sheets.Count

 For i = 1 To ShtCount - 1

 Worksheets(i).Activate
 LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

 Range("A2:P" & LastRow).Select

 Selection.Copy
 Sheets("Group Expenses").Activate

 LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Select

 'Required after first paste to shift active cell down one
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
 Loop


Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
.Range("A:M").EntireColumn.AutoFit

 Next i
End With

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Apart from the fact that code cannot work, which sheet are you trying to copy to?
You say one thing, but your code is doing something totally different.
 
Upvote 0
My apologies

Sheets("Group Expenses").Activate should have been Sheets("Consolidated Data")

I am trying to copy the data to Sheets("Consolidated Data")
 
Upvote 0
How about
VBA Code:
Sub howard()
   Dim i As Long
   Dim Ws As Worksheet
   
   Set Ws = Sheets("Consolidated Data")
   Ws.Range("A2:P" & Ws.Range("A" & Rows.Count).End(xlUp).Row).ClearContents

   For i = 1 To ActiveWorkbook.Sheets.Count - 1
      With Sheets(i)
         .Range("A2:P" & .Range("A" & Rows.Count).End(xlUp).Row).Copy
         Ws.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
      End With
   Next i
   Ws.Range("A:P").EntireColumn.AutoFit
End Sub
 
Upvote 0
Hi Fluff

Thanks for the help. Code works perfectly
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

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