Hi all,
Hoping someone can help.
The code below is used to append all data from various workbooks into a single master worksheet. This works fine when all the workbooks being appended contain the worksheet in question but runs into an error if a workbook does not contain the specific worksheet which is sometime the case. I would like the macro to run through the process and move on to the next file if the worksheet does not exist in the current workbook.
Any help would be greatly appreciated.
Thank you
Sub Append_data_Overtime()
Dim folderpath As String, filepath As String, filename As String
folderpath = Worksheets("Menu").Range("C2") & "\"
filepath = folderpath & "*.xls*"
filename = Dir(filepath)
Dim lastrow As Long, lastcolumn As Long
Do While filename <> ""
Workbooks.Open (folderpath & filename)
Worksheets("OvertimeByPost").Select
Rows("1:4").Select
Selection.Delete Shift:=xlUp
lastrow = Worksheets("OvertimeByPost").Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = Worksheets("OvertimeByPost").Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
erow = Worksheets("OvertimeByPost").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("OvertimeByPost").Select
ActiveSheet.Paste Destination:=Worksheets("OvertimeByPost").Range(Cells(erow, 1), Cells(erow, 14))
filename = Dir
Loop
Application.DisplayAlerts = True
End Sub
Hoping someone can help.
The code below is used to append all data from various workbooks into a single master worksheet. This works fine when all the workbooks being appended contain the worksheet in question but runs into an error if a workbook does not contain the specific worksheet which is sometime the case. I would like the macro to run through the process and move on to the next file if the worksheet does not exist in the current workbook.
Any help would be greatly appreciated.
Thank you
Sub Append_data_Overtime()
Dim folderpath As String, filepath As String, filename As String
folderpath = Worksheets("Menu").Range("C2") & "\"
filepath = folderpath & "*.xls*"
filename = Dir(filepath)
Dim lastrow As Long, lastcolumn As Long
Do While filename <> ""
Workbooks.Open (folderpath & filename)
Worksheets("OvertimeByPost").Select
Rows("1:4").Select
Selection.Delete Shift:=xlUp
lastrow = Worksheets("OvertimeByPost").Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = Worksheets("OvertimeByPost").Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
erow = Worksheets("OvertimeByPost").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("OvertimeByPost").Select
ActiveSheet.Paste Destination:=Worksheets("OvertimeByPost").Range(Cells(erow, 1), Cells(erow, 14))
filename = Dir
Loop
Application.DisplayAlerts = True
End Sub