VBA & Macro Problems

How to Change a UserForm Image on ListBox Selection in Excel VBA

Ayan MasoodAyan Masood Nov 10, 2026 869 views

Question details

The user wants to dynamically update an image displayed on an Excel VBA UserForm based on the current selection in a ListBox.

Change a UserForm Image When an Excel ListBox Selection Changes
Product
Excel (VBA)
Device & OS
not provided
Scenario
Creating an interactive UserForm where selecting a name from a ListBox automatically loads and displays the corresponding image from a local folder.
Observed behavior
The current VBA code only loads the image during the UserForm initialization phase and fails to update when the user selects a different item in the ListBox.
Before you start

Ensure that your image files are saved in an accessible local folder (e.g., a subfolder named 'Images' in the same directory as your workbook) and that your workbook is saved as a Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Implement the ListBox_Change Event to Update Images Dynamically

By moving the image loading logic from the initialization event to the ListBox change event, the UserForm will update the picture every time a new item is clicked.

When an image is loaded inside the UserForm_Initialize event, it only executes once when the form is first opened. To make the image dynamic, you must trigger the code whenever the ListBox selection changes.

The ListBox_Change event is the perfect place for this. By combining it with the LoadPicture function, you can dynamically build the file path based on the selected item's text and fetch the correct image file.

1
Access the VBA Editor

Open the VBA Editor by pressing Alt + F11, then double-click your UserForm in the Project Explorer to open the visual designer.

2
Create the ListBox Change Event

Double-click your ListBox control on the UserForm. This will automatically generate and open the Private Sub ListBox1_Change() event subroutine in the code window.

3
Declare Variables

Inside the subroutine, declare your variables by typing: Dim selectedName As String and Dim imagePath As String.

4
Retrieve the Selected Value

Assign the current value of the ListBox to your variable using: selectedName = ListBox1.Value.

5
Construct the Image Path

Build the full file path for the image. Assuming images are stored in an 'Images' subfolder within the workbook's directory, type: imagePath = ThisWorkbook.Path & "\Images\" & selectedName & ".jpg".

6
Load the Picture with Error Handling

Add an If statement using the Dir function to ensure the file exists before loading. Type: If Dir(imagePath) <> "" Then Image1.Picture = LoadPicture(imagePath) Else MsgBox "Image not found", vbExclamation End If.

Implement the ListBox_Change Event to Update Images Dynamically
Preventing Run-Time Errors: Using the Dir function is crucial. If the user selects a name that doesn't have a corresponding image file, the Dir check prevents VBA from throwing a fatal 'File not found' run-time error.
Advanced VBA Support in WPS Spreadsheet

Build Dynamic UserForms seamlessly with WPS Office

WPS Spreadsheet offers robust VBA and Macro support, allowing you to create interactive UserForms, automate repetitive tasks, and manage dynamic data seamlessly just like in Microsoft Excel.

  1. 1. Download and Install: Download and install the free version of WPS Office on your computer.
  2. 2. Open Your Workbook: Open your Macro-Enabled Workbook (.xlsm) using WPS Spreadsheet.
  3. 3. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click 'Visual Basic' to access the VBA editor.
  4. 4. Insert the Code: Open your UserForm, insert your ListBox and Image controls, and paste the ListBox1_Change code just as you would in traditional Excel VBA.
Fully compatible with Microsoft Excel (.xlsm) macro-enabled workbooks.Comprehensive VBA editor support for designing advanced UserForms with ListBoxes and Images.Lightweight software architecture that executes VBA code smoothly without freezing.Free to download and easy to transition from other Office suites.
QA img-9

Frequently Asked Questions

Why does the LoadPicture function throw a 'File not found' error?

This usually happens if the constructed image path is incorrect or the file does not actually exist in the folder. Always use Debug.Print imagePath to verify the exact path string in the Immediate Window, and use the Dir function to check file existence before executing LoadPicture.

Can I load images directly from an Excel worksheet instead of a local folder?

Loading images directly from a worksheet into a UserForm is complex and generally requires utilizing Windows APIs or exporting the chart/shape object as a temporary image file first. Using a dedicated local image folder is the standard and most reliable method in VBA.

How do I make the loaded image fit the Image control perfectly?

Select the Image control on your UserForm, go to the Properties window (press F4), and change the PictureSizeMode property to fmPictureSizeModeZoom (value 3). This resizes the image to fit the control while maintaining its original aspect ratio.