Load an Excel ActiveX Image Control from Cell Values with VBA
Question details
Compare a user's input with image names in cells A1:D4 and load the matching file into an existing worksheet Image control.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- Image filenames are stored in a worksheet range, and the user enters a name to display the corresponding picture in an ActiveX control.
- Observed behavior
- The VBA code produces a type mismatch when handling the values returned by the multi-cell range.
Make sure A1:D4 contains image filenames, including extensions, and that the files are stored in a known folder. Check the worksheet code name and ActiveX Image control name in the VBA editor; ActiveX controls require desktop Excel on Windows.
Compare each cell value in a Variant array and load the matching image
Use this approach to fix the type mismatch and display a matching file in the worksheet's existing ActiveX Image control.
A multi-cell Range.Value returns a two-dimensional array in a Variant. Assigning that entire array to a single String causes a type mismatch; compare the input with individual array elements instead.
The example assumes that the cell values are complete filenames and that all image files are in the same folder. It does not use cell comments.
In desktop Excel, press Alt+F11, then select Insert > Module.
Paste this code into the module: Sub LoadMatchingImage() Dim imageNames As Variant Dim userInput As String Dim imagePath As String Dim imageFolder As String Dim r As Long, c As Long imageFolder = "C:\Images\" userInput = Trim$(InputBox("Enter an image filename, including its extension:")) If userInput = "" Then Exit Sub imageNames = Sheet1.Range("A1:D4").Value For r = LBound(imageNames, 1) To UBound(imageNames, 1) For c = LBound(imageNames, 2) To UBound(imageNames, 2) If Not IsError(imageNames(r, c)) Then If StrComp(Trim$(CStr(imageNames(r, c))), userInput, vbTextCompare) = 0 Then imagePath = imageFolder & Trim$(CStr(imageNames(r, c))) If Dir$(imagePath) = "" Then MsgBox "Image file not found: " & imagePath Else Set Sheet1.OLEObjects("Image1").Object.Picture = LoadPicture(imagePath) End If Exit Sub End If End If Next c Next r MsgBox "No matching image name was found in A1:D4." End Sub
Replace C:\Images\ with the actual image folder, keeping the trailing backslash. Replace Sheet1 with the worksheet's VBA code name and Image1 with the ActiveX control name shown in its Properties window.
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). In Excel, open Developer > Macros, select LoadMatchingImage, click Run, and enter a filename from A1:D4.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday workbook editing to WPS. It does not directly fix Excel ActiveX or VBA issues; test any macros and controls before relying on them in WPS.

Frequently Asked Questions
Why does assigning the range to a String cause a type mismatch?
Sheet1.Range("A1:D4").Value returns a two-dimensional array. Store it in a Variant, then use row and column indexes to read each individual value.
How do I find the ActiveX Image control's name?
In desktop Excel on Windows, select Developer > Design Mode, right-click the Image control, and choose Properties. Use the value of its Name property in OLEObjects("Image1").
What should I enter when the macro prompts for an image name?
Enter a filename that matches a value in A1:D4, including its extension, such as photo.jpg. The example compares names without regard to letter case.
Why does the macro find the name but not load the picture?
Check that the file exists in the folder specified by imageFolder, that the cell includes the correct extension, and that LoadPicture supports the file's image format.
Can I run this ActiveX solution in Excel for the web or on a Mac?
No. The worksheet ActiveX Image control solution requires desktop Excel on Windows. Excel for the web does not run VBA macros, and Excel for Mac does not support ActiveX controls.




