excel vba请问为什么每次第一次on error goto 都可以到ma,但以后都不可以了? 5
xpath1="D:\lala\"xpath2="D:\文件\88\"Setfso=CreateObject("scripting.filesystemobject")S...
xpath1 = "D:\lala\"
xpath2 = "D:\文件\88\"
Set fso = CreateObject("scripting.filesystemobject")
Set f = fso.getfolder(xpath2)
Set fc = f.Files
myfile = Dir(xpath1 & "*.jpg")
Do While myfile <> ""
many = fc.Count
If Mid(myfile, 1, InStr(myfile, "_") - 1) ="111" Then
On Error GoTo ma
fso.movefile xpath1 & myfile, xpath2
GoTo ba
ma: Err.Clear
myfile1 = Left(myfile, Len(myfile) - 4) & many & ".jpg"
Name (xpath1 & myfile) As (xpath1 & myfile1)
fso.movefile xpath1 & myfile1, xpath2
ba:End If
myfile = Dir
If myfile = "" Then
Exit Do
End If
Loop
这是一个把文件从lala移到88中的操作,但是如果发现该文件与88的某文件重名,就将lala中的这个文件更名,然后在移到88中,可是运行后发现,第一次可以,以后就报错:文件未找到。
我想不通,为什么
On Error GoTo ma
fso.movefile xpath1 & myfile, xpath2
在第一次出错时,都跳至ma,而第二次就不跳了?(注:lala中与88重名的文件不止一个) 展开
xpath2 = "D:\文件\88\"
Set fso = CreateObject("scripting.filesystemobject")
Set f = fso.getfolder(xpath2)
Set fc = f.Files
myfile = Dir(xpath1 & "*.jpg")
Do While myfile <> ""
many = fc.Count
If Mid(myfile, 1, InStr(myfile, "_") - 1) ="111" Then
On Error GoTo ma
fso.movefile xpath1 & myfile, xpath2
GoTo ba
ma: Err.Clear
myfile1 = Left(myfile, Len(myfile) - 4) & many & ".jpg"
Name (xpath1 & myfile) As (xpath1 & myfile1)
fso.movefile xpath1 & myfile1, xpath2
ba:End If
myfile = Dir
If myfile = "" Then
Exit Do
End If
Loop
这是一个把文件从lala移到88中的操作,但是如果发现该文件与88的某文件重名,就将lala中的这个文件更名,然后在移到88中,可是运行后发现,第一次可以,以后就报错:文件未找到。
我想不通,为什么
On Error GoTo ma
fso.movefile xpath1 & myfile, xpath2
在第一次出错时,都跳至ma,而第二次就不跳了?(注:lala中与88重名的文件不止一个) 展开
2个回答
展开全部
因为你没有使用Resume [Next]返回。
Sub ss()
'……你程序的其他部分
xpath1 = "D:\lala\"
xpath2 = "D:\文件\88\"
Set fso = CreateObject("scripting.filesystemobject")
Set f = fso.getfolder(xpath2)
Set fc = f.Files
myfile = Dir(xpath1 & "*.jpg")
Do While myfile <> ""
many = fc.Count
If Mid(myfile, 1, InStr(myfile, "_") - 1) = "111" Then
On Error GoTo ma
fso.movefile xpath1 & myfile, xpath2
On Error GoTo 0
End If
myfile = Dir
Loop
'……你程序的其他部分
Exit Sub
ma: Err.Clear
myfile1 = Left(myfile, Len(myfile) - 4) & many & ".jpg"
Name (xpath1 & myfile) As (xpath1 & myfile1)
fso.movefile xpath1 & myfile1, xpath2
Resume Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询