vba 如何强制忽略/继续超过 1004 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21176638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force ignore/continue past a 1004 error?
提问by bsapaka
Basically I have this sub which inserts pictures into my worksheet:
基本上我有这个将图片插入到我的工作表中的子程序:
ActiveCell.Select
Dim picname As String
picname = ActiveCell.Value
ActiveCell.Offset(-1, 0).Select
Dim picture
Set picture = ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "\Images\" & picname & ".jpg")
I am running this sub with Application.Run inside of a loop. If there is no picture in the path folder, I get "Run-time error 1004: Unable to get the Insert property of the Picture class", and the loop stops. How do I bypass the error so that the loop can continue?
我在循环内使用 Application.Run 运行这个子程序。如果路径文件夹中没有图片,我会收到“运行时错误 1004:无法获取图片类的插入属性”,并且循环停止。如何绕过错误以便循环可以继续?
回答by Floris
The On Error
construct exists for this kind of thing. It comes with various option:
该On Error
构造存在于此类事情中。它带有各种选项:
On Error Goto label
Once your code encounters this statement, it will jump to label:
if an error occurs. It is useful if you need to do some error handling - especially if there are multiple places where things can go wrong but they always result in the same "need to fix something" routine. You can use Err.Number
or Err.Description
to find out what caused the error.
一旦你的代码遇到这个语句,它就会跳转到label:
如果发生错误。如果您需要进行一些错误处理,它很有用 - 特别是如果有多个地方可能出错,但它们总是导致相同的“需要修复某些东西”例程。您可以使用Err.Number
或Err.Description
找出导致错误的原因。
On Error Resume Next
Is useful when you have a single line that might cause an error - but if it does you want to ignore and keep going. This is most likely the right thing to do here.
当您有一行可能会导致错误时很有用 - 但如果确实如此,您想忽略并继续。这很可能是在这里做的正确的事情。
Don't forget to put
不要忘记放
On Error Goto 0
After you have passed the "danger", otherwise your code will (within the scope in which you issued the initial command) continue ignoring errors. Dangerous, that.
在您通过“危险”之后,否则您的代码将(在您发出初始命令的范围内)继续忽略错误。危险,那个。
So your code would be modified to
所以你的代码将被修改为
On Error Resume Next
Set picture = ActiveSheet.Pictures.Insert(ThisWorkbook.Path & "\Images\" & picname & ".jpg")
On Error GoTo 0
回答by Siddharth Rout
How do I bypass the error so that the loop can continue?
如何绕过错误以便循环可以继续?
Error Handling in suchcases is required when you have no other option or you are expecting an error and want to still continue. In your case, simply use a DIR command to check if the file exists and then only insert the picture. For example.
如果您别无选择,或者您预计会出现错误并希望继续继续,则需要在这种情况下进行错误处理。在您的情况下,只需使用 DIR 命令检查文件是否存在,然后仅插入图片。例如。
If Dir(ThisWorkbook.Path & "\Images\" & picname & ".jpg") <> "" Then
'
'~~> Your code to insert a picture
'
End If
The problem with
问题在于
On Error Resume Next
Set picture = ActiveSheet.Pictures.Insert....
On Error GoTo 0
is that in the later part of the code, you will have to add another line
是在代码的后面部分,您将不得不添加另一行
If Not picture = Nothing then
in case you are interacting with that object. Where as in the DIR
case, all your relevant code is sandwiched between IF/EndIF
and you will not require additional checks.
如果您正在与该对象进行交互。在这种DIR
情况下,您的所有相关代码都夹在中间IF/EndIF
,您不需要额外的检查。
回答by ron
insert
插入
on error resume next
before the loop
在循环之前