So I am writing a code in VBA that opens up all files in a document and copies and pastes the information from each document. The code is set up to open up every document, but itself. My dilemma is that I want the code to open ever document that has been modified after the last day the main file has been modified. Basically I want to compare two dates with one date staying the same and the other changing after every loop (new document every loop). My code is below and any help or suggestion would be much appreciated. Thanks!
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim Filepath As String
Dim DateMaster As Date
Dim DateReflections As Date
Filepath = "Path of folder where all the documents are"
MyFile = Dir(Filepath)
DateReflections = FileDateTime(Filepath)
DateMaster = FileDateTime("Filepath of master document I'm comparing to")
Do While Len(MyFile) > 0
If MyFile = "zmasterfile.xlsm" Then
Exit Sub
If DateReflections < DateMaster Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
Range("B4:N4").Copy
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Reflections").Range(Cells(erow, 2), Cells(erow, 14))
MyFile = Dir
Loop
End Sub