否则如果是FileExists()

时间:2014-11-11 10:38:55

标签: loops excel-vba if-statement vba excel

我需要检查是否存在每天生成的文件(周末和公共/银行假日除外)。每天的文件名都是基于日期的,所以如果我要在星期天运行它,我需要宏来执行以下操作。

我需要它来检查文件是否存在(它在星期日不会出现),然后从文件名中的日期减1天,然后测试它是否再次存在并继续循环直到找到确实存在的文件。

到目前为止,我已经获得了大部分代码,只是在使用循环部分:

Dim i As Integer
Dim Yday As Date
Dim YdayYear As Integer
Dim YdayMonth As Integer
Dim YdayDay As Integer
Dim CopyPath As String
Dim PastePath As String

i = 1
Yday = DateAdd("d", -i, Date)
YdayYear = DatePart("yyyy", Yday)
YdayMonth = DatePart("m", Yday)
YdayDay = DatePart("d", Yday)
CopyPath = "ABC\" & YdayYear & YdayMonth & YdayDay & ".csv"
PastePath = "XYZ\" & YdayYear & YdayMonth & YdayDay & ".csv"

If FileExists(CopyPath) Then
FileCopy Source:=CopyPath, Destination:=PastePath
    Else: i = i + 1

这是我陷入困境的地方。如何重新运行 If FileExists(CopyPath)部分代码?

3 个答案:

答案 0 :(得分:1)

也许是做一个while循环:

Do While Not FileExists(CopyPath)
    i = i +1
    Yday = DateAdd("d", -i, Date)
    YdayYear = DatePart("yyyy", Yday)
    YdayMonth = DatePart("m", Yday)
    YdayDay = DatePart("d", Yday)
    CopyPath = "ABC\" & YdayYear & YdayMonth & YdayDay & ".csv"
    PastePath = "XYZ\" & YdayYear & YdayMonth & YdayDay & ".csv"
Loop

FileCopy Source:=CopyPath, Destination:=PastePath

答案 1 :(得分:1)

如果我理解你的问题,可以使用Dir()函数检查文件是否存在:

If Dir(CopyPath) <> "" Then
    FileCopy Source:=CopyPath, Destination:=PastePath
Else
    i = i + 1
End If

答案 2 :(得分:1)

我会建议类似于亚历克斯建议的while循环,但不同并且稍微扩展一下你的实际要求,并且可以认为它不是传统for / while意义上的循环。首先,为了避免while循环继续进行,应该应用一些计数器和限制,以防没有文件存在。如果你想通过一系列各种复制和粘贴路径自动遍历,我还可以重新使用它,我取出了“循环”部分,并把它放在一个单独的函数中:

    Sub main()
        Dim vSuccess As Boolean
        Dim iterLimit As Integer ' iteration Limit, as in max number of times we want to go through
        Dim i As Integer
        Dim vDate As Date
        Dim copyFolder As String
        Dim pasteFolder As String

        iterLimit = 30 'for easier future followup, could be given directly into the function call
        i = 1 'same as above
        vDate = Date 'same as above
        copyFolder = "ABC\" 
        pasteFolder = "XYZ\" 

        vSuccess = IfDoesExist(copyFolder, pasteFolder, vDate, i, iterLimit) 'put it into the function IfDoesExist

        If vSuccess Then 'if the funciton returns True a match was found and the file copied
            MsgBox "Success, the file was copied"
        Else 'if not then do something
            MsgBox "No file found"
        End If

    End Sub



    Function IfDoesExist(copyFolder As String, pasteFolder As String, vDate As Date, i As Integer, iterLimit As Integer)
        Dim Yday As Date
        Dim YdayYear As Integer
        Dim YdayMonth As Integer
        Dim YdayDay As Integer
        Dim CopyPath As String
        Dim PastePath As String


        Yday = DateAdd("d", -i, vDate)
        YdayYear = DatePart("yyyy", Yday)
        YdayMonth = DatePart("m", Yday)
        YdayDay = DatePart("d", Yday)
        CopyPath = copyFolder & YdayYear & YdayMonth & YdayDay & ".csv"
        PastePath = pasteFolder & YdayYear & YdayMonth & YdayDay & ".csv"

        If iterLimit > 0 Then
            If Dir(CopyPath) <> "" Then
                FileCopy Source:=CopyPath, Destination:=PastePath
                vStatus = True 'we have a match
            Else 'if the file doesn't exist we want to rerun all of the above with a new i and iterLimit
                iterLimit = iterLimit - 1
                i = i + 1
                'Ok i know the next line of code may seem odd, but you will get True or False.
                'When the function stops calling itself the vStatus is either true because a 
                'match was eventually found, or false if it ws not. The value then travels back
                'through the calls/stack and ends up in the main programme.
                '
                'put in a breakpoint an take a look at the locals if you want to see the magic happen
                vStatus = IfDoesExist(copyFolder, pasteFolder, Date, i, iterLimit)
            End If
        Else
            vStatus = False 'if a match was never found within iterLimit calls
        End If

        IfDoesExist = vStatus 'return vStatus
    End Function

这可能是让事情过于复杂,但我很乐意这样做。虽然没有while或for循环,但是通过调用自身,函数将有效地工作。为了避免无限次迭代,每次调用时iterLimit都会减1。

在一系列路径中使用的模板未在代码中应用,但如果您查看Loop through each cell in a range of cells when given a Range object,您可能会了解如何完成此操作

修改了它在我的系统上运行的路径,但是如果您正在尝试并且它在您的系统上失败,请告诉我您遇到的错误

编辑:

总之回答你的问题,for循环应该这样做:

        Sub main2()
            Dim i As Integer
            Dim Yday As Date
            Dim YdayYear As Integer
            Dim YdayMonth As Integer
            Dim YdayDay As Integer
            Dim CopyPath As String
            Dim PastePath As String
            Dim vMax As Integer
            Dim vStatus As Boolean

            vMax = 30

            For i = 1 To vMax
                Yday = DateAdd("d", -i, Date)
                YdayYear = DatePart("yyyy", Yday)
                YdayMonth = DatePart("m", Yday)
                YdayDay = DatePart("d", Yday)
                CopyPath = "ABC\" & YdayYear & YdayMonth & YdayDay & ".csv"
                PastePath = "XYZ\" & YdayYear & YdayMonth & YdayDay & ".csv"

                If FileExists(CopyPath) Then
                    FileCopy Source:=CopyPath, Destination:=PastePath
                    vStatus = True
                    Exit For
                Else
                    i = i + 1
                End If
            Next

            If Not vStatus = True Then
                MsgBox "File Not found"
            End If
        End Sub