【问题标题】:VBA If file not foundVBA 如果找不到文件
【发布时间】:2012-11-17 04:44:14
【问题描述】:

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

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

简单。根据单元格 A13 中的标准更改注释 BG 图像,插入由 H2 生成的文件/路径。不过……

有时 H2 中的文件/路径不存在(因为 H2 是根据用户输入/选择创建的)。

我试过了:

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”的另一个生成文件/路径,对于没有关联图像的任何条目,这将是一个常量。它也不喜欢那样。

在方法尝试运行/查找 H2 中的内容之前,我找不到可以满足其需要的 else 表达式。

想法?

【问题讨论】:

    标签: vba excel excel-2007


    【解决方案1】:

    试试这个

    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
    

    【讨论】:

    • 显然它被称为“错误处理”。谁知道?谢谢你,先生。
    猜你喜欢
    • 2015-04-13
    • 2016-01-19
    • 2017-09-24
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多