Display Images Dynamically in Excel Based on Cell Selection
Have you ever wanted to instantly preview a picture that corresponds to a cell value without cluttering your worksheet? This tutorial shows you how to automatically display an image from a folder based on the active cell using Excel VBA — perfect for dashboards, product catalogs, inventory views, and interactive reports.
⭐ What This Tutorial Solves ?
In many Excel dashboards and interactive reports, users want to see a visual preview related to a selected value — without manually inserting or replacing images each time. This tutorial demonstrates how to automatically display an image based on the active cell value using Excel VBA.When a user selects a cell, Excel searches for a matching image file and displays it instantly.
✔ Typical Use Cases
• Product catalogs
• Inventory lists
• Interactive dashboards
• Educational worksheets
• Data-driven image previews This approach keeps worksheets clean while providing a highly interactive user experience.
Why this method is efficient
Images are loaded directly from a folder instead of being embedded into the Excel file. This keeps the workbook size small and ensures better performance, even with many images.
• Product catalogs
• Inventory lists
• Interactive dashboards
• Educational worksheets
• Data-driven image previews This approach keeps worksheets clean while providing a highly interactive user experience.
⭐ How It Works (Concept Overview) ?
The logic behind this solution is simple and efficient:
1. The selected cell value is read by VBA
2. Excel looks for an image file with the same name
3. The image is loaded from a predefined folder
4. The image is displayed dynamically on the worksheet at its original size.
5. If no match is found, the image is hidden 📌 No ActiveX controls are required — everything runs using standard VBA and image shapes.
2. Excel looks for an image file with the same name
3. The image is loaded from a predefined folder
4. The image is displayed dynamically on the worksheet at its original size.
5. If no match is found, the image is hidden 📌 No ActiveX controls are required — everything runs using standard VBA and image shapes.
⭐ Folder Structure & Image Naming Rules
For this solution to work correctly, image files must follow a consistent naming structure.
📂 Recommended Folder Structure
• Supported formats: .jpg
• Avoid spaces and special characters Example :
📂 Recommended Folder Structure
Workbook Folder
│
├── ExcelFile.xlsm
└── Images
├── Apple.jpg
├── Orange.jpg
└── Banana.jpg
📙 Image Naming Rule
• Image file names must match the cell values exactly • Supported formats: .jpg
• Avoid spaces and special characters Example :
| Cell Value | Image File | | ---------- | ---------- | | Apple | Apple.jpg | | Orange | Orange.jpg |
⭐ Preparing the Worksheet
Before writing the VBA code, we prepared the worksheet by adding an Image control (Image1) and keeping it hidden by default.
This image object will later be used to dynamically display pictures based on the selected cell value.
📌 This code should be placed in the Worksheet module, not in a standard module.
This allows A1 to act as a manual trigger or reset point.
• Column A selection logic
• The code assumes that the cell value corresponds to an image file name.
• If a matching image exists, the image will be shown.
• Horizontally, it is positioned next to the cell in column F (5 columns to the right of column A).
• Image File Lookup Logic This part of the code is responsible for checking whether a matching image file exists in the Images folder and loading it dynamically into the worksheet.
• This allows VBA to check whether a file exists in a specific folder.
• Trim(ActiveCell.Value) removes any leading or trailing spaces.
• If a matching .jpg file is found, it is loaded into Image1.
• This time, spaces inside the cell value are removed using Replace.
• This makes the solution more flexible when file names do not contain spaces.
• Image1_Click Procedure This procedure runs when the user clicks on the image.
• The image is hidden from the worksheet.
• The currently loaded picture is cleared from the Image control. This ensures that the image disappears immediately and releases the loaded file, keeping the worksheet clean and responsive.
RESULT : ⇩
This image object will later be used to dynamically display pictures based on the selected cell value.
⭐ VBA Code – Display Image Based on Cell Value
Below is the VBA code that detects the selected cell and displays the corresponding image dynamically.📌 This code should be placed in the Worksheet module, not in a standard module.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim fso As Object, shp As Shape
Set shp = Me.Shapes("Sekil")
If Target.Address = "$A$1" Or Target.Column <> 1 Then
Image1_Click
shp.Visible = False
Exit Sub
ElseIf Target.Address = "$A$" & ActiveCell.Row Then
Image1.Visible = True
Image1.Top = ActiveCell.Top
Image1.Left = ActiveCell.Offset(0, 5).Left
With shp
.Visible = True
'Dimensions of Arrow (relative to the cell)
.Height = Target.Height * 0.6
.Width = Target.Width * 0.2
'Location of Arrow
.Top = Target.Top + Target.Height / 2 - .Height / 2
.Left = Target.Width - (.Width + 3)
End With
End If
On Local Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(ThisWorkbook.Path & "\Images\" & Trim(ActiveCell.Value) & ".jpg") = True Then
Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Images\" & Trim(ActiveCell.Value) & ".jpg")
ElseIf fso.FileExists(ThisWorkbook.Path & "\Images\" & Replace(ActiveCell.Value, " ", "") & ".jpg") = True Then
Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Images\" & Replace(ActiveCell.Value, " ", "") & ".jpg")
Else
Image1.Visible = False
shp.Visible = False
End If
End Sub
📌 What the Code Does (Step-by-Step Explanation)
This Worksheet_SelectionChange event runs automatically whenever the user selects a different cell on the worksheet.Its main purpose is to dynamically display and reposition an Image control (Image1) based on the selected cell in column A.• Special case – Cell A1 If Target.Address = "$A$1" Or Target.Column <> 1 Then
Image1_Click
shp.Visible = False
Exit Sub
When cell A1 or a cell not in column A is selected, the code calls the Image1_Click procedure and stops further execution. The arrow shape (Sekil) is hidden.This allows A1 to act as a manual trigger or reset point.
• Column A selection logic
ElseIf Target.Address = "$A$" & ActiveCell.Row ThenWhen a cell in column A is selected:
• The code assumes that the cell value corresponds to an image file name.
• If a matching image exists, the image will be shown.
Image1.Visible = TrueThe Image control is made visible (it is normally hidden by default).
Image1.Top = ActiveCell.Top Image1.Left = ActiveCell.Offset(0, 5).Left• The image is vertically aligned with the selected cell.
• Horizontally, it is positioned next to the cell in column F (5 columns to the right of column A).
• Image File Lookup Logic This part of the code is responsible for checking whether a matching image file exists in the Images folder and loading it dynamically into the worksheet.
Set fso = CreateObject("Scripting.FileSystemObject")• A FileSystemObject (FSO) is created to interact with the file system.• This allows VBA to check whether a file exists in a specific folder.
If fso.FileExists(ThisWorkbook.Path & "\Images\" & Trim(ActiveCell.Value) & ".jpg") = True Then
Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Images\" & Trim(ActiveCell.Value) & ".jpg")
• First, Excel looks for an image file whose name exactly matches the selected cell’s value.• Trim(ActiveCell.Value) removes any leading or trailing spaces.
• If a matching .jpg file is found, it is loaded into Image1.
ElseIf fso.FileExists(ThisWorkbook.Path & "\Images\" & Replace(ActiveCell.Value, " ", "") & ".jpg") = True Then Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Images\" & Replace(ActiveCell.Value, " ", "") & ".jpg")• If no exact match is found, a fallback check is performed.
• This time, spaces inside the cell value are removed using Replace.
• This makes the solution more flexible when file names do not contain spaces.
• Image1_Click Procedure This procedure runs when the user clicks on the image.
Private Sub Image1_Click()
Image1.Visible = False
Image1.Picture = LoadPicture("")
End Sub
When the image is clicked: • The image is hidden from the worksheet.
• The currently loaded picture is cleared from the Image control. This ensures that the image disappears immediately and releases the loaded file, keeping the worksheet clean and responsive.
RESULT : ⇩
🔽Dynamic Arrow Indicator (Optional Visual Aid)
To make the selection behavior more visually clear, an arrow shape is added to the worksheet as a visual indicator.• When the worksheet is activated, VBA checks whether the arrow shape already exists.
• If it does not exist, the arrow is created automatically and named "Sekil".
• The arrow remains hidden by default.
• To guide the user visually, the arrow appears only when a selected cell in column A has a corresponding image. It automatically moves to point at the active cell.This small visual cue helps users immediately understand which cell is currently driving the displayed image, without adding any extra interaction or complexity.
• If it does not exist, the arrow is created automatically and named "Sekil".
• The arrow remains hidden by default.
• To guide the user visually, the arrow appears only when a selected cell in column A has a corresponding image. It automatically moves to point at the active cell.This small visual cue helps users immediately understand which cell is currently driving the displayed image, without adding any extra interaction or complexity.
The related VBA Codes : ↓
Private Sub Worksheet_Activate()
Dim shp As Shape
On Error Resume Next
Set shp = Me.Shapes("Sekil")
On Error GoTo 0
If shp Is Nothing Then
Set shp = Me.Shapes.AddShape( _
msoShapeRightArrow, 0, 0, 10, 10)
shp.Name = "Sekil"
shp.Visible = False
End If
End Sub The above code runs automatically when the worksheet is activated.
• The code checks whether a shape named "Sekil" already exists on the worksheet.
• If the shape is found, the procedure exits without creating a duplicate.
• If the shape does not exist, a Right Arrow shape is added.
• The shape is created using Left, Top, Width, and Height parameters.
• The arrow is initially hidden and will be shown later when required.
Later, the arrow can be resized and repositioned dynamically based on the selected cell.
• The code checks whether a shape named "Sekil" already exists on the worksheet.
• If the shape is found, the procedure exits without creating a duplicate.
• If the shape does not exist, a Right Arrow shape is added.
• The shape is created using Left, Top, Width, and Height parameters.
• The arrow is initially hidden and will be shown later when required.
Set shp = Me.Shapes.AddShape(msoShapeRightArrow, 0, 0, 10, 10)Parameter meanings :
| Parameter | Meaning | | -------------------- | -------------------------------------------- | | `msoShapeRightArrow` | Shape type (Right Arrow) | | `0` (Left) | Distance from the left edge of the worksheet | | `0` (Top) | Distance from the top edge of the worksheet | | `10` (Width) | Width of the arrow (points) | | `10` (Height) | Height of the arrow (points) |➡️ These values define the initial position and size of the arrow.
Later, the arrow can be resized and repositioned dynamically based on the selected cell.
How the Arrow Indicator Works (SelectionChange Flow) ?
The logic runs every time the user selects a different cell on the worksheet (Worksheet_SelectionChange). 1️⃣ Cell Selection Starts• A new cell is selected by the user.• Excel immediately triggers the SelectionChange event. 2️⃣ Initial Validation• The code first checks where the selection is made:
• If the selected cell is A1 (header)
• OR the selected cell is not in Column A ✅ Result :• The image (Image1) is hidden
• The arrow indicator (Sekil) is hidden
• The procedure exits immediately ➡️ No visual indicators are shown outside the data column. 3️⃣ Valid Cell in Column A• If a cell inside Column A (excluding A1) is selected:
• The worksheet image control (Image1) is prepared
• The arrow shape (Sekil) becomes active 4️⃣ Arrow Positioning Logic• The arrow is dynamically positioned relative to the selected cell :
• Vertically centered on the cell
• Horizontally aligned to point at the selected row
• Arrow dimensions are scaled based on the cell size :
• Height = 60% of cell height
• Width = 20% of cell width ➡️ This keeps the arrow responsive to different row heights. 5️⃣ Conditional Visibility The code checks whether an image file exists for the selected cell value :
• If a matching image is found :
• The image is loaded into Image1
• The arrow (Sekil) remains visible and points to the active cell
• If no image is found :
• The image is hidden
• The arrow is hidden ➡️ The arrow is shown only when a valid image exists. 6️⃣ Final Result🎯 The arrow indicator :
• Appears only for cells with matching images
• Moves dynamically with the selection
• Automatically hides itself when not needed.
📘 Conclusion
1. Does this method increase the Excel file size?
No. Images are loaded dynamically from an external folder using VBA.
Since images are not embedded into the workbook, the file size remains very small regardless of the number of images used.
2. Where should the images be stored?
All images must be placed in a folder named Images, located in thesame directory as the Excel file.
Each image file should be named to match the cell value (e.g. Bn101.jpg).
3. What happens if there is no matching image for a selected cell?
If no matching image file is found:
• The Image control is hidden
• The arrow indicator is also hidden
This prevents unnecessary visual clutter and keeps the worksheet clean.
• The Image control is hidden
• The arrow indicator is also hidden
This prevents unnecessary visual clutter and keeps the worksheet clean.
4. Does the arrow indicator appear for every selected cell?
No. The arrow becomes visible only when a valid cell in column A is selected and a matching image exists.
If the user selects another column or the header cell (A1), the arrow is automatically hidden.
5. How does the arrow adjust its position and size?
The arrow’s:
• Position is aligned to the active cell
• Size is calculated relative to the cell’s width and height
This ensures the arrow remains properly aligned even if row heights or column widths change.
• Position is aligned to the active cell
• Size is calculated relative to the cell’s width and height
This ensures the arrow remains properly aligned even if row heights or column widths change.
6. Can I use different image formats (PNG, GIF)?
Yes, with minor changes.
The current example checks for .jpg files, but the VBA logic can easily be extended to support .png, .gif, or multiple formats.
The current example checks for .jpg files, but the VBA logic can easily be extended to support .png, .gif, or multiple formats.
7. Is this solution compatible with different screen resolutions?
Yes. Because both the Image control and arrow indicator are positioned dynamically based on cell dimensions, the layout adapts well to different screen sizes and zoom levels.
8. Does this require macros to be enabled?
Yes. This solution relies on VBA events (SelectionChange, Activate), so macros must be enabled for it to work correctly.
✨ Supporting Multiple Image Formats (JPG, JPEG, GIF, BMP)
However, the same logic can easily be extended to support multiple image formats such as JPEG and GIF without changing the core structure of the VBA code. This flexibility allows you to organize images more freely while keeping the Excel file size small.Instead of checking only one file extension, we loop through a list of supported formats and load the first matching image found in the Images folder.
Note:
VBA’s LoadPicture function does not natively support PNG files. For maximum compatibility and stability, images should be stored in JPG or GIF format.
Dim fso As Object, shp As Shape
Dim imgPath As String, ext As Variant, found As Boolean
Dim v1 As String, v2 As String
Set shp = Me.Shapes("Sekil")
If Target.Address = "$A$1" Or Target.Column <> 1 Then
Image1_Click
shp.Visible = False
Exit Sub
ElseIf Target.Address = "$A$" & ActiveCell.Row Then
Image1.Visible = True
Image1.Top = ActiveCell.Top
Image1.Left = ActiveCell.Offset(0, 5).Left
With shp
.Visible = True
'Dimensions of Arrow (relative to the cell)
.Height = Target.Height * 0.6
.Width = Target.Width * 0.2
'Location of Arrow
.Top = Target.Top + Target.Height / 2 - .Height / 2
.Left = Target.Width - (.Width + 3)
End With
End If
Set fso = CreateObject("Scripting.FileSystemObject")
found = False
v1 = Trim(ActiveCell.Value)
v2 = Replace(ActiveCell.Value, " ", "")
' Supported formats
For Each ext In Array(".jpg", ".jpeg", ".bmp", ".gif") ' PNG deliberately excluded
imgPath = ThisWorkbook.Path & "\Images\" & v1 & ext
If fso.FileExists(imgPath) Then
Image1.Picture = LoadPicture(imgPath)
Image1.Visible = True
found = True
Exit For
End If
imgPath = ThisWorkbook.Path & "\Images\" & v2 & ext
If fso.FileExists(imgPath) Then
Image1.Picture = LoadPicture(imgPath)
Image1.Visible = True
found = True
Exit For
End If
Next ext
' If no supported image found, check for PNG
If Not found Then
imgPath = ThisWorkbook.Path & "\Images\" & Trim(ActiveCell.Value) & ".png"
imgPath = ThisWorkbook.Path & "\Images\" & Replace(ActiveCell.Value, " ", "") & ".png"
If fso.FileExists(imgPath) Then
Image1.Visible = False
shp.Visible = False
MsgBox "PNG images are not supported by VBA LoadPicture." & vbCrLf & _
"Please convert the image to JPG or GIF format.", _
vbInformation, "Unsupported Image Format"
Else
Image1.Visible = False
shp.Visible = False
End If
End If




Post a Comment