Niraj Baraili
Member
Hi All,
I am trying to consolidate different files with same heading which are dumped
in a same folder to a Consolidated file. For this i have written codes as below.
The codes are working fine untill there is no blank file in source folder.
If there is a blank file then the codes copy the data from previous file
two times. For example, Generator ID 1, Generator ID 2, Generator ID 3,
Generator ID 4, Generator ID 5 are source file. Generator ID 4 is blank.
When macro runs, it copies the data from the Generator ID 3 two times.If i
remove this blank file, the code runs perfectly.
Any help is appreciated.
I am trying to consolidate different files with same heading which are dumped
in a same folder to a Consolidated file. For this i have written codes as below.
The codes are working fine untill there is no blank file in source folder.
If there is a blank file then the codes copy the data from previous file
two times. For example, Generator ID 1, Generator ID 2, Generator ID 3,
Generator ID 4, Generator ID 5 are source file. Generator ID 4 is blank.
When macro runs, it copies the data from the Generator ID 3 two times.If i
remove this blank file, the code runs perfectly.
Any help is appreciated.
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
erow = Cells(Rows.Count, 1).End(xlUp).Row + 1
ActiveSheet.Paste Destination:=Worksheets("sheet1").Range(Cells(erow, 1), Cells(erow, 6))
Columns.AutoFit
myFile = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub