To delete all pictures on an Excel worksheet using VBA, you can run a short macro that loops through all shape objects and removes those identified as pictures. This clears embedded images, logos, or diagrams without affecting cell data or other objects—provided the worksheet isn’t protected or the macro lacks proper permissions. No external tools are required—just Excel with VBA enabled.
What You'll Need
- Microsoft Excel installed (Excel 2010 or later recommended)
- A workbook with pictures embedded on a worksheet
- Developer tab enabled in Excel (to access the VBA editor)
- Macro security settings permitting code execution (set to "Disable all macros with notification" or lower)
Step-by-Step
- Open the Excel workbook containing the sheet with pictures you want to delete.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the Project Explorer window (usually on the left), locate and expand your workbook (e.g., "VBAProject (YourWorkbook.xlsx)").
- Double-click the specific worksheet where you want to delete pictures (e.g., "Sheet1") to open its code module. Important: Do not use ThisWorkbook or a standard module unless you explicitly reference the sheet by name.
- Copy and paste the following VBA code into the code window for that sheet:
Sub DeleteAllPicturesOnSheet() Dim shp As Shape For Each shp In Me.Shapes If shp.Type = msoPicture Or shp.Type = msoEmbeddedOLEObject Or shp.Type = msoOLEControlObject Then ' Optional: narrow to actual picture types only On Error Resume Next shp.Delete On Error GoTo 0 End If Next shp MsgBox "All pictures deleted from " & Me.Name, vbInformation End SubThis code loops through every shape on the current sheet (
Merefers to the sheet where the code resides) and deletes objects identified as pictures or picture-like embedded objects. - Close the VBA editor by clicking the X or pressing Alt + Q.
- Press Alt + F8 to open the Macro dialog box.
- Select
DeleteAllPicturesOnSheetfrom the list, then click Run. - Confirm the message box appears, indicating successful deletion. Verify by returning to Excel—you should see no pictures on the sheet.
Common Mistakes
- Running code on the wrong sheet: If you paste the code into
ThisWorkbookor a standard module instead of the sheet’s code module,Mewon’t refer to the intended worksheet. UseWorksheets("SheetName").Shapesinstead if calling from a different module. - Over-deleting: Some shapes (e.g., charts, SmartArt, or buttons) may be misidentified. The provided code includes a broad filter; for stricter control, replace
msoEmbeddedOLEObjectandmsoOLEControlObjectwithmsoPictureonly. - Protected sheet: If the worksheet is protected, the macro will fail unless you unprotect it first (e.g.,
Me.Unprotect Password:="yourpassword"—omit the password argument if unprotected). - Macro security blocking: If Excel disables macros automatically, the code won’t run. Enable macros via File > Options > Trust Center > Trust Center Settings > Macro Settings.
Tips
- Backup first: Save a copy of your workbook before running the macro—accidental deletion is irreversible without undo (which fails after closing).
- Test on a copy: Run the macro on a duplicate worksheet or workbook to avoid losing important visuals.
- Refine the filter: For sheets with mixed objects, use
shp.ShapeRange.Type = msoPictureinside anIfblock to target only true pictures. - Speed it up: Add
Application.ScreenUpdating = Falseat the start andApplication.ScreenUpdating = Trueat the end to prevent flickering during large deletions. - Check for hidden pictures: Scroll or zoom out fully—some images sit behind cells or are partially obscured.
Troubleshooting
If the macro doesn’t delete any pictures or throws an error, follow these steps:
- Verify picture existence: Manually check the sheet—some images may be in header/footer, not on the sheet body. Use View > Header & Footer to inspect.
- Check shape types: Run a diagnostic macro to list all shapes:
Press Ctrl + G in the VBA editor to open the Immediate Window and review output.Sub ListShapes() Dim shp As Shape For Each shp In ActiveSheet.Shapes Debug.Print shp.Name & " - Type: " & shp.Type Next shp End Sub - Handle protected sheets: Unprotect the sheet (Review > Unprotect Sheet) before running the macro. If protected with a password, you must know it.
- Fix object type mismatches: If pictures are embedded OLE objects (e.g., Word documents), expand the condition to include
msoEmbeddedOLEObject. - Check for errors: If you see "Run-time error ‘1004’", ensure no shapes are locked or referenced by other objects (e.g., linked charts).
- Restart Excel: Occasionally, Excel’s object model gets stuck. Close and reopen the workbook, then retry.
Frequently Asked Questions
Will deleting pictures with VBA affect cell formatting?
No—deleting shapes only removes the picture objects themselves. Cell borders, shading, or data remain unchanged unless the picture was manually overlapping and altering visibility.Can I delete pictures from all sheets at once?
Yes—modify the code to loop through all worksheets. ReplaceMe.Shapes with For Each ws In ThisWorkbook.Worksheets: For Each shp In ws.Shapes ... Next shp: Next ws. Ensure each sheet isn’t protected.
Do I need administrator rights to run this macro?
No—only standard user permissions. However, your organization’s IT policies may restrict macro execution in corporate environments.What if the pictures are in a table or chart?
Pictures embedded within charts or as part of table formatting (e.g., background images) won’t be caught by this code. Charts require separate handling viaChartObject, and table backgrounds aren’t stored as shapes.
Is there a non-VBA way to delete all pictures?
Yes—use Go To Special: Press F5 > Special > Pictures > OK, then press Delete. This selects and removes all pictures on the active sheet without code.Why does my macro say "Object variable or With block variable not set"?
This usually means the shape object doesn’t exist when VBA tries to delete it (e.g., another macro already removed it). Wrap the delete line inOn Error Resume Next and reset error handling afterward, as shown in the main code.