【问题标题】:Else If for whether a FileExists()Else If 是否为 FileExists()
【发布时间】:2015-01-07 21:12:37
【问题描述】:

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

我需要它来检查文件是否存在(它不会在星期日),然后从文件名中的日期减去 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) 部分?

【问题讨论】:

  • 要继续搜索到什么时候?

标签: loops excel if-statement vba


【解决方案1】:

也许是一个 do 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

【讨论】:

  • 为什么选择在循环中定义变量列表,如果我在循环中只有 i = i +1 会不会行不通?
  • 当然你只需要i = i + 1Yday = DateAdd("d", -i, Date)和循环内的copypath部分。但是,您需要在复制路径中使用 Yday 的日期部分来执行复制路径。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我会建议与 Alex 建议的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 2011-03-17
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多