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