将标签和error handling程序放在正确的位置

我正在使用VBA中的error handling程序,并希望在我的代码中的error handling程序中使用,如许多专家在这里所build议的。

Sub test() For i =1 to 100 On Error GoTo ErrHand: Filename=Dir() Folder= Workbooks.open(Folder & Filename) Label1: Code Code Close the file ErrHand: Application.GetOpenFilename() GoTo Label1 Next i End Sub 

我正在寻找正常运行代码的困难。 我试图写一个文件,如果失败,我调用error handling程序,并提示select一个文件,然后我closures文件,并为下一个文件做同样的事情。 我面临的一个难题是打开的文件从不closures。 提前致谢。

除了Jeeped的优秀build议:

 Sub test() On Error GoTo ErrHand: For i =1 to 100 Filename=Dir() set Folder= Workbooks.open(Folder & Filename) 'Label1: Code Code folder.Close 'based on using 'folder', above next exit sub 'if you don't do this, code execution will go right into your error handler ErrHand: if err.Number = <something> Application.GetOpenFilename() 'GoTo Label1 Resume Next 'this is a simpler version, and doesn't require a label and another GoTo 'skips the line of code that errored, continues with the next line elseif err.number = <something else> '... Resume 'allows you to retry the same statement after making some changes endif 'Next i End Sub