使用Excel从目录插入图像

时间:2019-03-06 10:52:15

标签: excel vba

我想根据单元格中的内容从文件夹中插入图像

示例

单元格A1的单词为“ ABC001”

我希望单元格B1从目录中插入图像-图像名称=“ ABC001.JPG”

我找到了一些VBA代码可以为我执行此操作,但这仅适用于一个单元。 我希望它可以在整个专栏中工作

Private Sub Worksheet_Change(ByVal Target As Range)

Dim myPict As Picture
Dim PictureLoc As String

If Target.Address = Range("A2").Address Then

ActiveSheet.Pictures.Delete

PictureLoc = "\\ca-sbs-01\t\Shared\ExcelImages\" & Range("A2").Value & ".jpg"

With Range("B2")
    Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
    RowHeight = myPict.Height

    myPict.Top = .Top
    myPict.Left = .Left
    myPict.Placement = xlMoveAndSize
End With

End If

End Sub

1 个答案:

答案 0 :(得分:0)

也许你在追求

Private Sub Worksheet_Change(ByVal Target As Range)

Dim myPict As Picture
Dim PictureLoc As String

    On Error GoTo EH
    Application.EnableEvents = False
    If Target.Column = 1 Then

        'Pictures.Delete

        PictureLoc = "\\ca-sbs-01\t\Shared\ExcelImages\" & Target.Value2 & ".jpg"


        With Target.Offset(, 1)
            Set myPict = ActiveSheet.Pictures.Insert(PictureLoc)
            .RowHeight = myPict.Height
            myPict.Top = .Top
            myPict.Left = .Left
            myPict.Placement = xlMoveAndSize
        End With

    End If
EH:
    Application.EnableEvents = True
End Sub