Macro to Consolidate Data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,743
Office Version
  1. 2019
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
 

Some videos you may like

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
Office Version
  1. 365
Platform
  1. Windows
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.
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,743
Office Version
  1. 2019
Platform
  1. Windows
My apologies

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

I am trying to copy the data to Sheets("Consolidated Data")
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
Office Version
  1. 365
Platform
  1. Windows
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
 

howard

Well-known Member
Joined
Jun 26, 2006
Messages
5,743
Office Version
  1. 2019
Platform
  1. Windows
Hi Fluff

Thanks for the help. Code works perfectly
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,931
Messages
5,621,666
Members
415,849
Latest member
PhoenixRising2015

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
Top