VBA如果找不到文件

我不做很多的VBA。 我有一些代码:

If Target.Address = "$H$1" Then Range("A13").Comment.Shape.Fill.UserPicture Range("H2").Value End If 

简单。 更改根据单元格A13中的条件评论BG图像,插入由H2生成的文件/path。 然而…

有时H2中的文件/path不存在(因为H2是基于用户input/select创build的)。

我试过这个:

 If Target.Address = "$H$1" Then Range("A13").Comment.Shape.Fill.UserPicture Range("H2").Value Else Range("A13").Comment.Shape.Fill.UserPicture Range("H6").Value End If 

其中H6是另一个生成的“NOIMAGE.jpg”的文件/path,对于没有关联图像的条目来说,这是一个常数。 它也不喜欢那个。

我找不到其他expression式,它会在方法试图运行/findH2中的内容之前做它所需要的。

想法?

尝试这个

 If Target.Address = "$H$1" Then On Error Resume Next Range("A13").Comment.Shape.Fill.UserPicture Range("H2").Value If Err.Number <> 0 Then Range("A13").Comment.Shape.Fill.UserPicture Range("H6").Value End If On Error GoTo 0 End If