2012-09-23 30 views
0

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

+0

您可以使用'Worksheet_SelectionChange(ByVal Target As Range)'事件,并且在这种情况下您可以启动图片。顺便说一句当你点击单元格时,你打算在哪里显示图片?在评论中?在用户表单中? –

+0

我想在评论中显示它。 –

+1

好吧,我可以给你整个代码,但我想让你先试试它?我们手动插入图像。右键单击单元格并单击“显示注释”。然后右键点击评论的边界并点击'格式评论'。在出现的格式框中,在“颜色和线条|填充|填充效果”下选择您的图像。一旦你了解了如何添加图像,只需记录一个宏并将其嵌入到'Worksheet_SelectionChange(ByVal Target As Range)'中。试一试,如果你被卡住了,那么显示你试过的代码和你得到的错误,我们会从那里拿走它? –

回答

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 
相关问题