Ivan Morales
Ivan Morales

Reputation: 11

VBA: how to tell if/else statement in a do loop to continue

So I've written a code that goes through a folder opens up documents, copies information, and pastes in a master file located somewhere else. The problem I am having is that I want to add an if, else statement in order to compare the date the last time master file was modified to the documents in the other folder. When it doesn't meet the if statement I want the loop to skip that document and continue to compare the next document. Not sure how to proceed with this, the code can be seen below.

Sub Datecheck()
    Dim MyFile As String
    Dim erow
    Dim Filepath As String
    Dim otherfiledate As Date
    Dim zmasterdate As Date
    Filepath = "folder of where all files are located"
    MyFile = Dir(Filepath)
    zmasterdate = FileDateTime("location of zmasterdate")

    Do While Len(MyFile) > 0
        otherfiledate = FileDateTime(Filepath & "\" & MyFile)
        If otherfiledate > zmasterdate Then

            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

        Else

            End Sub

        End If
    Loop 
End Sub

Upvotes: 1

Views: 387

Answers (1)

Sorceri
Sorceri

Reputation: 8033

You already do the check so you don't need an else Clause. Just move your MyFile = Dir to the end of the loop

Sub Datecheck()
Dim MyFile As String
Dim erow
Dim Filepath As String
Dim otherfiledate As Date
Dim zmasterdate As Date
Filepath = "folder of where all files are located"
MyFile = Dir(Filepath)
zmasterdate = FileDateTime("location of zmasterdate")

Do While Len(MyFile) > 0
    otherfiledate = FileDateTime(Filepath & "\" & MyFile)
    If otherfiledate > zmasterdate Then

        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))

    End If
    MyFile = Dir
Loop
End Sub

Upvotes: 1

Related Questions