To work with a picture in Excel VBA, you insert it as a Shape object and then control it through its properties and methods. The shortest working set of steps is: open the Visual Basic Editor, add a module, write a macro that uses ActiveSheet.Shapes.AddPicture (or Pictures.Insert for a linked image), set properties like .Top, .Left, .Width, and .Height, then run the macro. This guide walks through that process, explains the difference between embedded and linked pictures, and covers common pitfalls and fixes. Note that menu labels and dialog wording can differ slightly by Excel version, device, and region.
What You'll Need
- Excel for Windows or Mac (the VBA editor is not available in Excel for the web).
- A picture file saved locally (JPG, PNG, BMP, GIF, or similar).
- Basic familiarity with the Visual Basic Editor (VBE) and how to run a macro.
- Macro security settings that allow macros to run (you may need to enable macros for the workbook).
Step-by-Step
- Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or Option+F11 (Mac). If the Developer tab is visible, you can also click Developer > Visual Basic.
- Insert a module. In the VBE, go to Insert > Module. A new blank module appears in the Project Explorer.
- Write a macro to insert a picture. Use the
Shapes.AddPicturemethod. Example:
TheSub InsertPicture() Dim pic As Shape Set pic = ActiveSheet.Shapes.AddPicture( _ Filename:="C:\Images\logo.png", _ LinkToFile:=msoFalse, _ SaveWithDocument:=msoTrue, _ Left:=100, Top:=100, Width:=-1, Height:=-1) End SubWidth:=-1andHeight:=-1arguments tell Excel to use the picture's original dimensions. SetLinkToFile:=msoTrueif you want the picture to update when the source file changes; setSaveWithDocument:=msoTrueto embed it in the workbook. - Run the macro. Press F5 while the cursor is inside the subroutine, or go to Run > Run Sub/UserForm. The picture appears on the active sheet.
- Position and size the picture. After insertion, you can set properties:
You can also lock the aspect ratio by settingWith pic .Top = 50 .Left = 50 .Width = 200 .Height = 150 End With.LockAspectRatio = msoTruebefore changing width or height. - Reference an existing picture. If a picture is already on the sheet, you can loop through
ActiveSheet.Shapesand check.Type = msoPictureto find it. Example:Dim shp As Shape For Each shp In ActiveSheet.Shapes If shp.Type = msoPicture Then shp.Top = 10 shp.Left = 10 End If Next shp - Delete a picture. Use
pic.Deleteor loop and delete shapes of typemsoPicture. - Save your workbook. Save as a macro-enabled workbook (.xlsm) to keep the VBA code.
Common Mistakes
- Using the wrong file path. A relative path or a missing file causes run-time error 1004. Use a full path and verify the file exists.
- Forgetting to set
LinkToFileandSaveWithDocument. If both aremsoFalse, the picture is inserted but not saved with the document, so it may disappear when the workbook is reopened. - Assuming
Pictures.Insertembeds the image.Pictures.Insertcreates a linked picture by default; useShapes.AddPicturewithSaveWithDocument:=msoTruefor an embedded image. - Not handling errors. Wrap file operations in error handling to give a clear message if the file is missing or locked.
- Confusing Shape and Picture objects. A picture on a worksheet is a
Shapeobject; there is no separatePictureobject in the Excel object model for worksheet pictures.
Tips
- Use
msoTrueandmsoFalseconstants (from the Office library) rather thanTrue/Falsefor clarity. - Set
.Placement = xlMoveAndSizeif you want the picture to resize with cells. - To keep the aspect ratio when resizing, set
.LockAspectRatio = msoTruebefore changing width or height. - Use
Application.ScreenUpdating = Falsebefore inserting many pictures, then set it back toTrueto speed up the macro. - For pictures that need to be updated regularly, consider linking instead of embedding to keep file size down.
Troubleshooting
If the macro does not run, check that macros are enabled and that the workbook is saved in a macro-enabled format (.xlsm). If you get error 1004, verify the file path and that the image format is supported. If the picture appears but is not saved, check the SaveWithDocument argument. If the picture is distorted, set LockAspectRatio to msoTrue before resizing. If the macro runs slowly with many pictures, disable screen updating during the operation.
Frequently Asked Questions
Can I insert a picture from a URL?
Yes, Shapes.AddPicture can accept a URL, but the image must be accessible and the method may be slow or fail if the server requires authentication. For reliability, download the image first.
How do I insert a picture into a specific cell?
Pictures float above cells; they are not contained in cells. You can position a picture over a cell by setting its Top and Left properties to match the cell's coordinates, or use the Range.Top and Range.Left properties.
What is the difference between Shapes.AddPicture and Pictures.Insert?
Shapes.AddPicture gives you full control over linking and embedding via arguments. Pictures.Insert is a shortcut that inserts a linked picture by default and returns a Picture object (which is a type of Shape).
Can I resize a picture to fit a range?
Yes, set the picture's Top, Left, Width, and Height to match the range's Top, Left, Width, and Height properties.
Why does my picture disappear when I reopen the workbook?
This usually happens when the picture was inserted with LinkToFile:=msoTrue and SaveWithDocument:=msoFalse, so only a link is saved. Use SaveWithDocument:=msoTrue to embed the image.
Can I insert multiple pictures at once?
Yes, loop through a list of file paths and call Shapes.AddPicture for each, adjusting position each time (for example, offsetting Top by the picture height plus a gap).