VBA - Loop to consolidate lists from multiple workbooks into a consolidated table

anthonygws

New Member
Joined
Jun 17, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to code a macro that will consolidate data from multiple workbooks into another one, detecting the last row to select only the relevant range, then detecting the last row in the consolidation table to paste after the last row.

I get stuck at the copy paste lines and will be eternally grateful if one could help me.

Here is the code

Sub Consolidate()

Dim Path As String
Dim Filename As String
Dim Sheet As Worksheet
Dim Sht As Worksheet, DstSht As Worksheet
Dim LstRow As Long, LstCol As Long, DstRow As Long

Path = "C:\Users\UserName\Documents\SourceFolder\"

Filename = Dir(Path & "*.xlsx")

Do While Filename <> ""

Workbooks.Open Filename:=Path & Filename, ReadOnly:=True

LstRow = ActiveWorkbook.Sheets(1).Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row

DstRow = ThisWorkbook.Sheets(1).Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row

DstRow = DstRow + 1

ActiveWorkbook.Sheets("Time").Range(Cell(A2) : Z & LstRow).Copy

ThisWorkbook.Sheets("Consolidation").Range("A & DstRow : Z & DstRow + LstRow").Paste

Workbooks(Filename).Close

Filename = Dir()

Loop

End Sub

_____________


Thank you very much !

Anthony
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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