Hi Guys,
I was wondering if anyone can help with the below. I have this Macro working and it's returning the data needed from each of the files, however it only works if I keep the button to run the macro in the 'raw data' worksheet. Ideally I would like this button to sit on the sheet named 'overview' that would then run the macro and paste the data into the raw data sheet? When I've tried it it references this line as being the error on the macro.
Many thanks in advance
ActiveSheet.Paste Destination:=Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1))
Sub LoopThroughDirectory()
Dim Filepath As String
Dim erow
Filepath = Application.ActiveWorkbook.Path
MyFile = Dir(Filepath + "\*.*")
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Rows("2:" & Format(erow)).EntireRow.ClearContents
Do While Len(MyFile) > 0
If MyFile = "ZMasterFileDTL.xlsm" Then
Exit Sub
End If
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open (Filepath & "\" & MyFile)
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Range("A2:U900").Copy
Sheets("Sheet1").Visible = False
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1))
MyFile = Dir
Application.DisplayAlerts = True
Loop
End Sub
I was wondering if anyone can help with the below. I have this Macro working and it's returning the data needed from each of the files, however it only works if I keep the button to run the macro in the 'raw data' worksheet. Ideally I would like this button to sit on the sheet named 'overview' that would then run the macro and paste the data into the raw data sheet? When I've tried it it references this line as being the error on the macro.
Many thanks in advance
ActiveSheet.Paste Destination:=Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1))
Sub LoopThroughDirectory()
Dim Filepath As String
Dim erow
Filepath = Application.ActiveWorkbook.Path
MyFile = Dir(Filepath + "\*.*")
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet1.Rows("2:" & Format(erow)).EntireRow.ClearContents
Do While Len(MyFile) > 0
If MyFile = "ZMasterFileDTL.xlsm" Then
Exit Sub
End If
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open (Filepath & "\" & MyFile)
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Range("A2:U900").Copy
Sheets("Sheet1").Visible = False
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Raw Data").Range(Cells(erow, 31), Cells(erow, 1))
MyFile = Dir
Application.DisplayAlerts = True
Loop
End Sub