Display Images Dynamically Based On Cell Contents

Display Images In  Worksheet With VBA Codes


         In this Excel template ,when a cell in column A is selected,  picture of the cell contents is shown in column F.
The picture in Images folder with the same name as value in the cell is displayed on the image control.
When the displayed image in column F is clicked , this image invisible.

💡 Important point is that ,names of the pictures with names of product in column A are same.

Construction steps of this template ;
✔ First, we added an image control called Image1 while Developer mode is active on the worksheet.


✔ We hide the image control for loading images based on the value of the selected cell in column A with that VBA codes:
        Sheets("Data").Image1.Visible = False

We added the images to the Images folder .

✔ We added the following codes to the Worksheet_SelectionChange method of  Data sheet to display the images on the image control according to the record in the cell :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static OldCell As Range
If Target.Address = "$A$1" Then
Image1_Click
Exit Sub
ElseIf Target.Address = "$A$" & ActiveCell.Row Then
Range("F1").Value = ActiveCell.Value
Image1.Visible = True
Image1.Top = ActiveCell.Top
Image1.Left = ActiveCell.Offset(0, 5).Left
End If

On Local Error Resume Next
Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Images\" & Range("F1").Value & ".jpg")
If Err Then Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Images\Yok.jpg")

If Not OldCell Is Nothing Then
    OldCell.Interior.ColorIndex = xlColorIndexNone
End If
If Target.Address = "$A$" & ActiveCell.Row Then
Target.Interior.ColorIndex = 6
Set OldCell = Target
End If
End Sub




2 comments:

  1. Hello,
    This file, work with excel 2003 or excel 2010 ?

    Thanks for your answer.

    ReplyDelete