带有转到和倍增错误的VBA错误处理程序
因此,我似乎无法让这项工作,我不明白为什么。它说的是带有转到和倍增错误的VBA错误处理程序,vba,ms-access,Vba,Ms Access,因此,我似乎无法让这项工作,我不明白为什么。它说的是error 53,但当我将intNumber更改为一个不存在的数字时,它工作正常 我认为问题是同时发生多个错误 Function Download_Location() As String 'kill the file as well Dim intNumber As Integer, strOther As String intNumber = 1 fReshstart: Download_Location = "C
error 53
,但当我将intNumber
更改为一个不存在的数字时,它工作正常
我认为问题是同时发生多个错误
Function Download_Location() As String
'kill the file as well
Dim intNumber As Integer, strOther As String
intNumber = 1
fReshstart:
Download_Location = "C:\Users\" & Environ$("Username") & "\Downloads\MSaccessfile" & intNumber & ".xlsx"
On Error GoTo Errhandler
Kill Download_Location
Errhandler:
Select Case Err
Case 53: 'why wont it pick error 53
Err.Clear
MsgBox "it worked"
Exit Function 'no file to delete
Case 70: intNumber = intNumber + 1
Err.Clear
GoTo fReshstart
End Select
End Function
我在使用Kill时遇到了很多复杂的错误场景,并创建了一个漂亮的函数来处理它。这是给任何感兴趣的人的。这很简单,但不必进行如此疯狂的涉及Kill的错误处理。希望它能帮助你和其他人走出困境 首先将此功能添加到模块中:
Function KillAndWasKilled(strFilePath As String) As Boolean
'attempt to kill a file and report success; trying to head off all the oddball stuff that can happen in functions when kill is used
On Error GoTo ErrHandler
Kill strFilePath
KillAndWasKilled = True
Exit Function
ErrHandler:
KillAndWasKilled = False
End Function
现在替换这个:
Kill Download_Location
用这个
if dir(Download_Location) <> "" then 'the file exists; if it doesnt exist then there's nothing to attempt to kill!
if KillAndWasKilled(Download_Location) = false then 'something went wrong when trying to kill the file
err.Raise -666, , "Unable to Delete " & Download_Location
end if
end if
如果dir(下载位置)“,则文件存在;如果它不存在,那就没有什么可杀的!
如果KillAndWasKilled(下载位置)=false,则“在尝试终止文件时出现了错误”
错误:Raise-666,“无法删除”&下载位置
如果结束
如果结束
发现问题问题问题是我需要使用错误转到-1上的清除错误处理程序
Errhandler:
Select Case Err
Case 53: 'used when their is no file to kill
On Error GoTo -1
Case 70: intNumber = intNumber + 1 'used when the file is opened already so new file is required to be built
On Error GoTo -1
GoTo fReshstart
End Select
结束函数在
Errhandler:
之前没有退出函数
,因此错误处理程序也会在成功的终止
后执行。因此这些解决方案都不起作用。因为我希望能够生成MSaccessfile1、MSaccessfile2、MSaccessfile3,额外。。。但问题发生在一个错误发生时。错误处理程序似乎不想处理更多的错误。哦,我意识到它不会告诉你失败的原因,但我不会做任何盲杀,通常只有几个原因。该文件不存在、已锁定或没有权限。确保首先检查文件是否存在(如果dir(theFilePath)=“”,则表示文件不存在)