How to Change a UserForm Image on ListBox Selection in Excel VBA
Question details
The user wants to dynamically update an image displayed on an Excel VBA UserForm based on the current selection in a ListBox.

- 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.
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).
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.
Open the VBA Editor by pressing Alt + F11, then double-click your UserForm in the Project Explorer to open the visual designer.
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.
Inside the subroutine, declare your variables by typing: Dim selectedName As String and Dim imagePath As String.
Assign the current value of the ListBox to your variable using: selectedName = ListBox1.Value.
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".
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.

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. Download and Install: Download and install the free version of WPS Office on your computer.
- 2. Open Your Workbook: Open your Macro-Enabled Workbook (.xlsm) using WPS Spreadsheet.
- 3. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click 'Visual Basic' to access the VBA editor.
- 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.

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.




