我需要检查是否存在每天生成的文件,周末和公共(public)/银行假日除外。每天的文件名都基于日期,所以如果我要在星期天运行它,我需要宏来执行以下操作。
我需要它来检查文件是否存在(它不会在星期日),然后从文件名中的日期中减去 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) 部分?
最佳答案
也许是一个 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
关于loops - Else If 是否为 FileExists(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26862892/