使用Excel从目录插入图像

问题描述 投票:0回答:1

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

单元格A1有“ABC001”字样

我希望单元格B1从目录中插入图像 - image name =“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
excel vba
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.