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

  1. 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.
  2. Insert a module. In the VBE, go to Insert > Module. A new blank module appears in the Project Explorer.
  3. Write a macro to insert a picture. Use the Shapes.AddPicture method. Example:
    Sub 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 Sub
    The Width:=-1 and Height:=-1 arguments tell Excel to use the picture's original dimensions. Set LinkToFile:=msoTrue if you want the picture to update when the source file changes; set SaveWithDocument:=msoTrue to embed it in the workbook.
  4. 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.
  5. Position and size the picture. After insertion, you can set properties:
    With pic
        .Top = 50
        .Left = 50
        .Width = 200
        .Height = 150
    End With
    You can also lock the aspect ratio by setting .LockAspectRatio = msoTrue before changing width or height.
  6. Reference an existing picture. If a picture is already on the sheet, you can loop through ActiveSheet.Shapes and check .Type = msoPicture to 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
  7. Delete a picture. Use pic.Delete or loop and delete shapes of type msoPicture.
  8. 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 LinkToFile and SaveWithDocument. If both are msoFalse, the picture is inserted but not saved with the document, so it may disappear when the workbook is reopened.
  • Assuming Pictures.Insert embeds the image. Pictures.Insert creates a linked picture by default; use Shapes.AddPicture with SaveWithDocument:=msoTrue for 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 Shape object; there is no separate Picture object in the Excel object model for worksheet pictures.

Tips

  • Use msoTrue and msoFalse constants (from the Office library) rather than True/False for clarity.
  • Set .Placement = xlMoveAndSize if you want the picture to resize with cells.
  • To keep the aspect ratio when resizing, set .LockAspectRatio = msoTrue before changing width or height.
  • Use Application.ScreenUpdating = False before inserting many pictures, then set it back to True to 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).