Excel Macro to Insert an Image: VBA Steps and Options

VBA & macros · Updated November 20, 2026

To insert an image with an Excel macro, use VBA’s Shapes.AddPicture method. The example below opens a file picker, inserts the selected picture at cell B2, and fits it inside that cell while preserving its proportions; it embeds the image in the workbook rather than linking to the original file.

How the image-inserting macro works

Excel pictures are floating objects layered over the worksheet grid, not contents stored inside a cell like text or a number. A macro can place a picture using a cell’s position and dimensions, so it appears aligned with that cell. The cell acts as an anchor and a convenient sizing reference, but the picture remains a separate worksheet object.

This procedure uses a file picker rather than a fixed image path. That makes it useful when you want to run the same macro for different image files. It targets B2 on the active worksheet, centers the picture in that cell, and reduces the picture to fit if necessary. If the chosen picture is already smaller than the cell, the code does not enlarge it.

VBA macro to insert and fit a picture

In desktop Excel for Microsoft 365 on Windows, activate the worksheet where you want the image, then run this macro. It accepts common image formats such as PNG, JPEG, GIF, and BMP. Save a copy of the workbook before running any macro; VBA actions cannot be undone with Ctrl+Z.

Option Explicit

Sub InsertImageAtB2()
    Dim ws As Worksheet
    Dim target As Range
    Dim imagePath As Variant
    Dim pic As Shape
    Dim targetRatio As Double

    ' Use the active worksheet and cell B2 as the placement area.
    Set ws = ActiveSheet
    Set target = ws.Range("B2")

    ' Ask the user to choose an image; cancel exits the procedure.
    imagePath = Application.GetOpenFilename( _
        FileFilter:="Image Files (*.png;*.jpg;*.jpeg;*.gif;*.bmp),*.png;*.jpg;*.jpeg;*.gif;*.bmp", _
        Title:="Select an image")
    If VarType(imagePath) = vbBoolean Then Exit Sub

    ' Embed the selected file and begin at its original dimensions.
    Set pic = ws.Shapes.AddPicture( _
        Filename:=CStr(imagePath), _
        LinkToFile:=msoFalse, _
        SaveWithDocument:=msoTrue, _
        Left:=target.Left, _
        Top:=target.Top, _
        Width:=-1, _
        Height:=-1)

    ' Fit and center the picture without changing its aspect ratio.
    With pic
        .LockAspectRatio = msoTrue
        targetRatio = target.Width / target.Height
        If .Width / .Height > targetRatio Then
            .Width = target.Width
        Else
            .Height = target.Height
        End If
        .Left = target.Left + (target.Width - .Width) / 2
        .Top = target.Top + (target.Height - .Height) / 2
        .Placement = xlMove
    End With
End Sub

The GetOpenFilename dialog returns either a selected file name or a Boolean value when the user cancels. The macro exits in the latter case. In AddPicture, LinkToFile:=msoFalse and SaveWithDocument:=msoTrue tell Excel to store the picture in the workbook. Width and height values of -1 request the image’s original dimensions before the fitting code runs.

How to add and run the macro

  1. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to keep the VBA code in that file. A standard .xlsx workbook cannot store VBA macros.
  2. Open the Visual Basic Editor with Alt+F11 on Windows. In the editor, choose Insert > Module.
  3. Paste the code into the module, then return to Excel. If the Developer tab is hidden, enable it at File > Options > Customize Ribbon by selecting Developer.
  4. Run the macro from Developer > Macros, select InsertImageAtB2, and choose Run. Select a picture in the file dialog.

You can also run a macro from the Visual Basic Editor with the cursor inside its procedure by pressing F5. On a Mac, VBA macros are supported in desktop Excel, but keyboard shortcuts and interface labels can vary with the keyboard and Excel version. Use Tools > Macro > Visual Basic Editor where available, or find the Visual Basic Editor from the Developer tab. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros; open the file in desktop Excel to use this procedure.

Choose where and how the picture sits

To use a different anchor, change ws.Range("B2") to another cell reference, such as ws.Range("D5"). The macro then uses that cell’s top, left, width, and height to position the image. If you want the picture to start in a cell but retain its original size, remove the fitting and centering section and use only the coordinates in AddPicture.

The final line, .Placement = xlMove, makes the image move with its cells when rows or columns are repositioned, without automatically resizing it as cell dimensions change. Excel also offers xlMoveAndSize, which makes an object move and resize with cells, and xlFreeFloating, which keeps it independent of cell movement. These settings affect how the picture behaves after insertion; they do not put the image inside a cell.

If the target area is a merged range, use the merged range as the target and check that its width and height are what you expect. For a picture that should occupy several cells, replace the single-cell target with an appropriate range and use that range’s dimensions. A picture’s position is measured in points, so it can be aligned to worksheet cells even though its dimensions are not expressed as row or column counts.

Change the macro for repeated or fixed images

For a fixed image that should always be inserted, you can replace the file picker with a full path, for example C:\Images\logo.png, and remove the cancellation check. This is convenient for a personal workbook, but a path on one person’s computer may not exist for another user. A shared network location or a workbook-embedded image may be more suitable when the file must travel.

For a batch workflow, a macro can loop through rows, read image paths from a worksheet column, and place each image beside its matching record. The paths must be valid and accessible, and the procedure needs a clear rule for handling missing files, duplicate pictures, and existing images. For large batches, consider whether every image needs to be embedded: workbook size can grow substantially, especially when source pictures are high resolution.

To replace the current picture each time, the macro would also need to identify and delete the old shape before adding a new one. Deleting by position alone is risky because a worksheet may contain other shapes. A safer pattern is to assign the picture a distinctive name when it is created and have the procedure look for that specific name before replacing it.

Common problems and fixes

  • The macro does not appear in the list: Confirm that the code is in a standard module and that the workbook is saved as .xlsm. Code placed in a worksheet module may not appear as an ordinary runnable macro.
  • Excel blocks the macro: Check whether the workbook came from an untrusted source and follow your organization’s security policy. Do not enable macros in a file just because it contains a picture macro; only run code you trust.
  • The picture is in the wrong place: The procedure uses the active worksheet. Activate the intended sheet before running it, or change the code to refer to a named worksheet. Confirm that the target cell is not hidden in a way that makes the placement surprising.
  • The image looks stretched: Keep .LockAspectRatio = msoTrue and use the fit logic rather than assigning unrelated width and height values. The code fits within the target bounds and centers the result, leaving unused space on one side when the cell and image have different shapes.
  • The workbook is unexpectedly large: Embedded pictures travel with the workbook, which is useful for portability but increases file size. Resize or compress large source images before inserting them if their full resolution is unnecessary.

Embedded pictures, linked pictures, and Place in Cell

An embedded picture is saved with the workbook. It remains available if the original file is moved or deleted, but it contributes to workbook size. A linked picture instead refers to an external file, which can reduce what is stored in the workbook but depends on that file staying at an accessible path. The sample macro embeds the image deliberately to avoid a broken link when the workbook is shared.

Some Microsoft 365 builds also provide a Place in Cell option under Insert > Pictures. That feature is different from the floating shape created by Shapes.AddPicture. Availability and exact options can depend on the Excel build, and the VBA example here does not invoke the in-cell picture feature. If a picture must behave as cell content, check whether Place in Cell is available in your version; if you need repeatable placement, sizing, or batch processing, VBA may be more appropriate.

For a one-off image, using Insert > Pictures is usually simpler than writing a macro. A macro is most useful when the same placement task happens repeatedly, when image files come from a changing list, or when a workbook needs a consistent layout generated on demand.

Safety and platform notes

VBA macros can change workbook content and may run code beyond inserting a picture. Only run macros from sources you trust, and follow workplace rules about macro-enabled files. Before testing a new procedure on important data, make a separate copy. Excel’s Undo history does not reverse VBA changes, so Ctrl+Z cannot reliably remove a picture or restore other edits made by a macro.

The code is intended for desktop Excel on Windows and uses standard Excel VBA and Office drawing constants. Excel for Mac also supports VBA, though file-picker behavior, menus, permissions, and keyboard shortcuts can differ. The code uses a dialog rather than a Windows-specific file path, which makes it more portable than a hard-coded path, but confirm that the image format and macro permissions work in the Mac version you use.

Frequently Asked Questions

Can a macro insert a picture into a specific Excel cell?
Yes. A macro can position a picture using a cell’s coordinates, as the example does with B2. The picture is still a floating shape anchored over the grid, not ordinary cell content.

How do I make the picture move and resize with cells?
Set the shape’s Placement property to xlMoveAndSize. The example uses xlMove instead, so the image follows cells when they move but does not resize with them.

Can I insert images automatically from file paths in a column?
Yes. VBA can read each path, add the corresponding picture, and position it beside the related row. A batch macro should check that each file exists and decide what to do when a path is missing; the file-picker example handles just one image per run.

Will the image still appear if I send the workbook to someone else?
With the sample’s embedded-picture settings, the image is stored in the workbook and normally travels with it. A linked image depends on the external file remaining available at the expected location.

Can I run this macro in Excel for the web?
No. Excel for the web does not run VBA macros. Open the macro-enabled workbook in the desktop version of Excel to run the procedure.

Why can’t I undo the picture insertion with Ctrl+Z?
Excel clears or bypasses its normal undo history when VBA changes the workbook. Save a backup copy before running a macro, and delete the inserted shape manually or rerun a purpose-built cleanup macro if needed.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.