将文件夹中的徽标插入Excel

时间:2017-02-23 16:16:16

标签: excel image vba excel-vba

我正在寻找一种方法将文件夹中的图片插入excel中的特定单元格。该文件夹包含数百张图片。我不熟悉excel宏或VBA。代码应该工作的方式是我将图片名称键入单元格,结果应从该文件夹中提取具有相同名称的图片并将其放入另一个单元格。插入的图片也应调整大小。如果有人可以请给我一些指导,说明如何做到这一点。谢谢。我很抱歉我的英语很差,因为这是我的第二语言。

1 个答案:

答案 0 :(得分:0)

假设您输入的图像名称为" myImage.jpg"进入工作表的单元格A1。确保它是扩展名的全名。将其放入工作表代码中。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim imagePath, fullImagePath, newImageLoc As String

    imagePath = "C:\YourFileLocationPath\" 'set this to where your images are located.
    If Target.Address = "$A$1" Then 'Assumming the cell you are entering your image is in A1 or change to whatever cell you want entering in.
        fullImagePath = imagePath & Target.Value
        newImageLoc = Target.Offset(, 2).Address ' this moves the new image location to the cell you enter your image name two columns to the right.
        'Adjusts image size, change as needed
        With ActiveSheet.Pictures.Insert(fullImagePath)
            With .ShapeRange
                .LockAspectRatio = msoTrue
                .Width = 75
                .Height = 100
            End With
            .Left = ActiveSheet.Range(newImageLoc).Left
            .Top = ActiveSheet.Range(newImageLoc).Top
            .Placement = 1
            .PrintObject = True
        End With
        End
    End If
End Sub