VBA & Macro Problems

Load and Reset a Worksheet Picture with Excel VBA

Elise WilliamsElise Williams Nov 18, 2026 870 views

Question details

Select an image file, display it in a worksheet, save its path, and reset the picture using VBA.

Load and Reset a Worksheet Picture with Excel VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Use worksheet ActiveX buttons to load and clear a picture in a named worksheet shape.
Observed behavior
The original code suppresses errors, compares a String with False, and attempts to change a worksheet shape picture incorrectly.
Before you start

Use a desktop version of Excel that supports ActiveX controls, and save the workbook as a macro-enabled .xlsm file. On Sheet3, reserve a cell such as Z1 for the image path and enable macros only if you trust the workbook.

Solution 1Recommended

Replace the worksheet picture with VBA

Use ActiveX buttons to load a picture into a named worksheet shape and reset it to an empty placeholder.

GetOpenFilename returns a file path or the Boolean value False when the dialog is canceled, so the result must be stored in a Variant. The code replaces a worksheet shape; it does not set the Picture property of an ActiveX Image control.

This example uses the worksheet's own code module, a shape named img_Photo, and cell Z1. Change the cell reference if Z1 is already in use.

1
Create the picture placeholder

On Sheet3, use Insert > Shapes to draw a rectangle where the picture should appear. Open Home > Find & Select > Selection Pane and rename the rectangle img_Photo.

2
Add the ActiveX buttons

On Sheet3, select Developer > Insert > Command Button under ActiveX Controls and add two buttons. In Design Mode, use Developer > Properties to name them CommandButton1 and CommandButton2; set their captions to Load and Clear.

3
Add the VBA procedures

Right-click the Sheet3 tab, select View Code, and paste these procedures into the worksheet code module: Private Sub CommandButton1_Click() Dim filePath As Variant Dim oldShape As Shape Dim newShape As Shape filePath = Application.GetOpenFilename( _ "Image Files (*.jpg;*.jpeg;*.png;*.gif;*.bmp),*.jpg;*.jpeg;*.png;*.gif;*.bmp") If VarType(filePath) = vbBoolean Then Exit Sub Set oldShape = Me.Shapes("img_Photo") Set newShape = Me.Shapes.AddPicture( _ CStr(filePath), msoFalse, msoTrue, _ oldShape.Left, oldShape.Top, oldShape.Width, oldShape.Height) oldShape.Delete newShape.Name = "img_Photo" Me.Range("Z1").Value = CStr(filePath) End Sub Private Sub CommandButton2_Click() Dim oldShape As Shape Dim placeholder As Shape Set oldShape = Me.Shapes("img_Photo") Set placeholder = Me.Shapes.AddShape( _ msoShapeRectangle, oldShape.Left, oldShape.Top, _ oldShape.Width, oldShape.Height) placeholder.Fill.Visible = msoFalse placeholder.Line.Visible = msoFalse oldShape.Delete placeholder.Name = "img_Photo" Me.Range("Z1").ClearContents End Sub

4
Test both buttons

Return to Sheet3 and turn off Developer > Design Mode. Click Load, select an image, and confirm its path appears in Z1. Click Clear to remove the picture and path, then try Load again.

Replace the worksheet picture with VBA
Troubleshoot without suppressing errors: Do not use On Error Resume Next while testing. If an error appears, check that img_Photo exists on the same sheet as the buttons and that the selected file is accessible. The picture is resized to the placeholder's dimensions and may be distorted.
Free Microsoft Office alternative

Edit Excel Workbooks and Worksheet Pictures in WPS Office

WPS Office is a free, lightweight alternative for working with spreadsheets in a familiar interface. Open compatible Excel files and edit worksheet content as you move between office suites. WPS does not fix Excel ActiveX controls; VBA and ActiveX behavior may differ, so test any automation separately.

Insert and adjust worksheet pictures through the spreadsheet interfaceOpen and save compatible Microsoft Excel formats, including XLSXUse a familiar interface for an easier transition from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why should the file variable be a Variant?

Application.GetOpenFilename returns a file path when a file is selected and Boolean False when the dialog is canceled. A Variant can hold either result; check VarType before using the path.

Why does the macro say it cannot find img_Photo?

On the sheet containing the buttons, open Home > Find & Select > Selection Pane and check that the placeholder or picture is named img_Photo. The example uses Me.Shapes, so the shape must be on the same worksheet as the code.

Does clearing the picture also delete the saved file?

No. The Clear button replaces the worksheet picture with an empty placeholder and clears cell Z1. It does not delete the original image file.

Will this code work with an ActiveX Image control?

No. This example replaces a worksheet shape using Shapes.AddPicture. An ActiveX Image control is a different object and requires code that sets its Picture property.

Why don't the buttons run when I click them?

Turn off Developer > Design Mode and check that macros are enabled for the trusted workbook. ActiveX controls also require a supported desktop version of Excel.