Niraj Baraili
Member
Hi All,
I have multiple files in a folder. I need to open all the files one by one copy data from sheet1 and paste in a another workbook one below other. For this i have written VBA code as below:
But the problem is, in Destination file data is not pasting one below other from source file. Instead of this, data is pasting in Range("A2:E5") in destination file each time.
Any help on this appreciated.
----------------------------------------------------------------------------
Mod Edit: Code Tags added
I have multiple files in a folder. I need to open all the files one by one copy data from sheet1 and paste in a another workbook one below other. For this i have written VBA code as below:
Code:
Sub LoopthroughDirectory()
Dim myFile As String
Dim erow
Dim filepath As String
Dim Destfile As String
Dim myFileName As String
myFileName = "Consolidated File.xlsx"
Destfile = "D:\Consolidation\Output\"
filepath = "D:\Consolidation\Input\"
myFile = Dir(filepath)
Workbooks.Open (Destfile & "\" & myFileName)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While Len(myFile) > 0
Workbooks.Open (filepath & myFile)
'If There is no data in file then go the next file
If Range("A2").Value = "" Then
ActiveWorkbook.Close
Else
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWorkbook.Close
End If
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row + 1
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, 5))
myFile = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
But the problem is, in Destination file data is not pasting one below other from source file. Instead of this, data is pasting in Range("A2:E5") in destination file each time.
Any help on this appreciated.
----------------------------------------------------------------------------
Mod Edit: Code Tags added
Last edited by a moderator: