Hi There,
I have a master file with sheet1. A3 to C3 range is header.
I have some workbooks in a folder. All these have data in sheet1.
I am trying to consolidate all these workbooks one below each other without header since header is same.
I have below code but nothing moves an inch. Wonder what must be going wrong.
I tried a lot of threads before posting this. I was unable to figure the problem out
Thanks
Spg
I have a master file with sheet1. A3 to C3 range is header.
I have some workbooks in a folder. All these have data in sheet1.
I am trying to consolidate all these workbooks one below each other without header since header is same.
I have below code but nothing moves an inch. Wonder what must be going wrong.
VBA Code:
Sub ConsolidateData()
Dim wb As Workbook
Dim wsMaster As Worksheet
Dim wsSource As Worksheet
Dim FileName As String
Dim FolderPath As String
Dim LastRow As Long
'Set the folder path where the source files are located
FolderPath = "C:\Users\New folder"
'Set the reference to the Master workbook and worksheet
Set wb = ThisWorkbook
Set wsMaster = wb.Sheets("Sheet1")
'Clear all data except headers in Sheet1 of Master workbook
LastRow = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Row
wsMaster.Range("A4:C" & LastRow).ClearContents
'Loop through all Excel files in the specified folder
FileName = Dir(FolderPath & "*.xlsx*")
Do While FileName <> ""
'Open the source workbook and set the reference to the worksheet
Set wb = Workbooks.Open(FolderPath & FileName)
Set wsSource = wb.Sheets(1)
'Find the last row in the source worksheet
LastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
'Copy the data from the source worksheet to the Master worksheet
wsSource.Range("A4:C" & LastRow).Copy wsMaster.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
'Close the source workbook without saving changes
wb.Close False
'Get the next file name
FileName = Dir()
Loop
End Sub
I tried a lot of threads before posting this. I was unable to figure the problem out
Thanks
Spg