How to Load a Default Image When File is Missing in Excel VBA
Question details
The user needs an Excel VBA script that dynamically loads an image into a shape based on a cell value, but automatically displays a default fallback image if the requested file does not exist in the folder.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Loading images dynamically into Excel shapes using VBA triggered by cell value changes.
- Observed behavior
- If the specified image file is missing or incorrectly named, the VBA script throws a runtime error when calling the UserPicture method, causing the macro to halt unexpectedly.
Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and verify the exact folder paths for both your target images and your default fallback image.
Use Dir Function and Error Handling in Worksheet_Change
Use the VBA Dir function to check if the image path exists before applying the UserPicture method, and implement an error handler to catch unexpected issues.
By verifying the file path with the Dir function before attempting to load the image, you can safely instruct VBA to load a default image without triggering a runtime error. Adding an ErrorHandler provides a secondary safety net.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. In the Project Explorer on the left, double-click the specific Worksheet (e.g., Sheet1) where you want the dynamic image loading to occur.
Paste the following event code into the worksheet module: Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo ErrorHandler Dim path As String path = "C:\A\FOTO JBA\" & Range("O1").Value & ".JPG" If Dir(path) <> "" Then Shapes("Imagem 1").Fill.UserPicture path Else Shapes("Imagem 1").Fill.UserPicture "C:\A\FOTO JBA\0.JPG" End If Exit Sub ErrorHandler: Shapes("Imagem 1").Fill.UserPicture "C:\A\FOTO JBA\0.JPG" End Sub
Modify the folder path ("C:\A\FOTO JBA\"), the trigger cell reference (Range("O1")), the shape name ("Imagem 1"), and the default image file name ("0.JPG") to match your actual workbook setup.
Close the VBA editor and return to your worksheet. Change the value in the trigger cell (e.g., O1). If the corresponding image exists, it will load into the shape; if not, the default image (0.JPG) will appear.

Run Excel VBA Macros Smoothly with WPS Spreadsheet
WPS Spreadsheet offers robust, built-in support for VBA macros, allowing you to seamlessly run dynamic image-loading scripts and error handlers without any code modification.
- 1. Download and Install: Download WPS Office from the official website and complete the quick installation process.
- 2. Open Your Macro Workbook: Launch WPS Spreadsheet and open your existing Macro-Enabled Workbook (.xlsm).
- 3. Enable Macros: Click 'Enable Macros' if prompted by the security warning at the top of the screen.
- 4. Access the VBA Editor: Go to the Developer tab and click 'VBA Editor' to view, edit, or run your dynamic image scripts exactly as you would in Excel.

Frequently Asked Questions
Why do I get a Run-time error 1004 when loading an image in VBA?
This usually happens when the file path passed to the UserPicture method or Insert command does not exist or is misspelled. Using the Dir function to verify the file path beforehand prevents this error from occurring.
Can I apply this VBA script to multiple images on the same sheet?
Yes, you can expand the VBA code using a loop or multiple If...Else blocks. You just need to check the paths for each respective cell value and update the Fill.UserPicture property for the different shape names accordingly.
What image formats does Excel VBA support for the UserPicture method?
Excel VBA supports standard image formats like JPG, PNG, GIF, and BMP when using the UserPicture method to fill a shape. Make sure your file extension in the VBA string (e.g., ".JPG") strictly matches your image files.




