从excel中的文件夹动态显示图片

时间:2012-09-23 21:30:38

标签: excel vba excel-vba

我有一些带有一些值的单元格。此值是文件夹中图片的名称。当我点击一个单元格“值”时,我想显示一个名为“value”的正确图片。我想动态地执行它,以便添加图片会导致在单击具有其名称值的单元格时显示它。你能告诉我我应该从哪里开始吗?有没有教程显示如何做到这一点?我找到了很多,但他们在excel工作表中使用列表和存储图片。有没有例子?

1 个答案:

答案 0 :(得分:0)

Private Sub CommandButton2_Click()
           On Error Resume Next
          Dim imageFolder As String 'this is the folder where the image is located
    Dim imagePath As String


    Cells.Find("Code").Offset(1).Select
Range(Selection, Selection.End(xlDown)).Select
For Each cell In Selection
imageFolder = cell.Value
imagePath = "C:\Documents and Settings\kollol\My Documents\Quotes\Image\" & imageFolder

cell.Offset(0, 2).Select

ActiveSheet.Pictures.Insert(imagePath & "\" & "1.jpeg").Select ' here the name of the image is 1.jpg


 With Selection
.Placement = xlMoveAndSize
 .ShapeRange.LockAspectRatio = msoTrue
.ShapeRange.Width = ActiveCell.ColumnWidth
 .ShapeRange.Height = ActiveCell.RowHeight - 5
.ShapeRange.IncrementLeft 10.5
.ShapeRange.IncrementTop 4#
 End With

Next cell

End Sub