VBA Resize Pictures to Fit in Excel Cells Automatically

VBA & macros · Updated November 2, 2026

VBA Resize Pictures to Fit in Excel Cells Automatically

To resize pictures to fit inside Excel cells with VBA, run a macro that finds each picture, scales it proportionally to the cell beneath its upper-left corner, and centers it there. The macro below does that on the active worksheet; save a copy of your workbook first, because VBA macros cannot be undone with Ctrl+Z.

What “fit in a cell” means in Excel

In a standard Excel worksheet, an inserted picture is a floating object layered over the grid, not content stored inside a cell. It can cover several cells, sit partly outside the visible grid, or overlap text. A macro can size and position that object so it appears contained by a chosen cell, but it does not turn the picture into a cell value.

For this macro, the target is the cell—or merged area—under the picture’s upper-left corner. The picture is reduced or enlarged just enough to fit within that target while keeping its original proportions. Any unused space is left as padding, and the picture is centered. For example, a wide landscape photo in a tall, narrow cell will fit to the cell’s width and leave empty space above and below rather than being stretched out of shape.

VBA macro to fit pictures inside cells

On Windows desktop Excel for Microsoft 365, this macro processes pictures and linked pictures on the active worksheet. It leaves a small two-point margin around each image and uses the cell under each picture’s top-left corner as its target.

Sub FitPicturesToTopLeftCell()
    Dim ws As Worksheet
    Dim shp As Shape
    Dim target As Range
    Dim scaleW As Double
    Dim scaleH As Double
    Dim scaleFactor As Double
    Dim margin As Double

    Set ws = ActiveSheet
    margin = 2

    For Each shp In ws.Shapes
        If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Then
            Set target = shp.TopLeftCell

            If target.MergeCells Then
                Set target = target.MergeArea
            End If

            If target.Width > 2 * margin And target.Height > 2 * margin Then
                If shp.Width > 0 And shp.Height > 0 Then
                    scaleW = (target.Width - 2 * margin) / shp.Width
                    scaleH = (target.Height - 2 * margin) / shp.Height
                    scaleFactor = WorksheetFunction.Min(scaleW, scaleH)

                    shp.LockAspectRatio = msoTrue
                    shp.Width = shp.Width * scaleFactor
                    shp.Left = target.Left + (target.Width - shp.Width) / 2
                    shp.Top = target.Top + (target.Height - shp.Height) / 2
                    shp.Placement = xlMove
                End If
            End If
        End If
    Next shp
End Sub

The two-point margin is measured in points, the unit Excel uses for object dimensions. Change margin = 2 to a larger number for more breathing room, or set it to zero to use the available cell area fully. The macro does not crop pictures: it preserves the whole displayed image and fits it inside the target.

How to add and run the macro

First, save a working copy of the workbook. Macros cannot be reversed with Ctrl+Z, and running this code changes picture size and position. Then make sure the worksheet containing the pictures is active and follow these steps in desktop Excel for Windows:

  1. Press Alt+F11 to open the Visual Basic Editor.
  2. Choose Insert > Module in the editor.
  3. Paste the complete macro into the new module.
  4. Return to Excel, activate the worksheet to process, and press Alt+F8.
  5. Select FitPicturesToTopLeftCell and choose Run.

Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) if you want to keep the macro in the file. If Excel displays a security warning, only enable macros in a workbook you trust. To process another worksheet, activate it and run the macro again. It works on one active worksheet per run, not every sheet in the workbook.

How the sizing and positioning work

The code compares the picture’s current width and height with the target cell’s usable width and height. It calculates a width-based scale and a height-based scale, then uses the smaller value. That is the key to fitting without distortion: the picture is constrained by whichever cell dimension it would reach first. Locking the aspect ratio before changing the width keeps the image’s proportions intact.

After resizing, the macro calculates the leftover horizontal and vertical space and divides it evenly on both sides. This centers the picture rather than leaving it aligned to the cell’s upper-left edge. The calculation uses Excel’s actual cell and picture dimensions, so it does not rely on approximate pixel conversions or on a particular screen zoom.

A picture already smaller than its cell may be enlarged, because the code uses the same scale calculation whether the scale factor is above or below 1. If you want to prevent enlargement and only shrink oversized images, add a check that sets scaleFactor to 1 whenever it is greater than 1. That keeps small thumbnails at their original size while still reducing pictures that do not fit.

Choosing the right cell for each picture

Excel pictures float over the worksheet, so the target-cell rule matters. This macro uses TopLeftCell: if an image’s upper-left corner lies over B4, it is fitted to B4, even if most of the picture currently covers C4 or B5. That makes the macro predictable for pictures placed roughly in their intended cells, but it will not identify a destination from a nearby label or from the cell containing a filename.

Before running it, arrange each picture so its upper-left corner is over the cell where you want it to end up. If images are scattered across several cells, first move them into their intended starting positions. For a more specialized workbook, VBA can instead match each image to a known row or to a cell containing an ID, but that requires additional rules about how the pictures and records are associated.

Merged cells are handled by using the full merged area as the target. This is useful for a photo displayed across a merged block, but merged cells can make sorting, filtering, and table design less convenient. If the image area is part of a structured list, consider using an ordinary, consistently sized column instead.

Placement settings and later row or column changes

The line shp.Placement = xlMove tells Excel to move each picture with its cell while keeping the picture’s dimensions fixed. This is a cautious choice: changing a row’s height or a column’s width later will not automatically stretch the picture to refill the cell. Run the macro again after changing the grid if you want every image recentered and fitted to the new dimensions.

Excel also offers the xlMoveAndSize placement behavior, which makes an object move and resize with its cells. That may be useful when a worksheet’s row heights and column widths are stable, but resizing a picture with the grid can change how it fits and may not maintain the exact contained-and-centered result you want. Choose placement based on how the sheet will be edited, and check a few representative pictures after changing it.

Neither placement option makes the macro run automatically when a user adjusts a row or column. A normal worksheet change event is not a reliable trigger for every layout adjustment, so rerunning the macro is the straightforward approach. If the worksheet is frequently resized, test any event-based automation carefully and avoid making it run on every unrelated cell edit.

Common problems and limitations

  • The wrong cell is used: Check where the picture’s upper-left corner sits. The macro does not choose the cell with the largest overlap.
  • The picture is cropped already: The macro fits the picture’s current displayed shape. It does not restore parts removed with Excel’s Crop command.
  • A picture does not move as expected: Confirm you are running the macro on the worksheet that contains it, and that it is a picture or linked picture rather than a chart, icon, or grouped object.
  • Images look too small: A very wide or tall image must leave empty space in the other direction to preserve its proportions. Increase the cell size or crop the image deliberately if a fuller-looking thumbnail is more important than showing the entire picture.
  • Pictures overlap after running: The images may be assigned to the same target cell because their upper-left corners were over that cell. Move each one to its own intended cell before running again.

The macro processes the shapes Excel exposes on the worksheet. It does not recursively resize pictures inside grouped shapes, pictures embedded in chart elements, or objects on a different sheet. It also does not change row heights or column widths to make room; it adapts the pictures to the existing grid.

VBA versus Excel’s Place in Cell option

Some Microsoft 365 builds of Excel provide a Place in Cell option for pictures through Insert > Pictures. When available, it puts supported pictures into cells instead of leaving them as ordinary floating objects. This can be a better fit for a table that needs pictures to behave more like cell content, though feature availability and behavior can differ by Excel update channel and account.

Use VBA when you need to reposition or resize many existing floating pictures in a repeatable way, or when you need a controlled margin and centered layout. Consider Place in Cell when it is available and you are adding images to a compatible worksheet. These approaches are not identical: a macro adjusts floating shapes, while Place in Cell uses Excel’s in-cell picture feature. For one or two images, manually selecting a picture and resizing it may be simpler than enabling macros.

Windows, Mac, and Excel for the web

The macro uses Excel’s VBA object model and is intended for desktop Excel. On a Mac, VBA is available in desktop Excel, but the Visual Basic Editor and macro-launch shortcuts can differ from Windows. Use the Mac Excel menus to open the Visual Basic Editor or Macro dialog if the Windows shortcuts do not apply, then paste and run the same code. The exact key combination can also depend on the Mac keyboard and system settings.

Excel for the web does not run VBA macros. You can open and edit many macro-enabled workbooks there, but run this procedure in desktop Excel. Office Scripts are a separate automation option in supported Microsoft 365 environments; they use TypeScript rather than VBA and require a different implementation.

Frequently Asked Questions

Can VBA make a picture resize automatically when I resize a cell?
The macro fits pictures when you run it; it does not monitor row-height or column-width changes. Run it again after changing the grid. Excel’s move-and-size placement can make objects follow grid changes, but check the result because it may not preserve the same contained layout.

Will the macro stretch or distort my pictures?
No. It locks each picture’s aspect ratio and fits it inside the target area. This can leave blank space on two sides when the cell and picture have different proportions, which is expected when the whole picture must remain visible.

Can I fit a picture to a cell other than its upper-left cell?
Yes, but the macro needs a different targeting rule. For instance, it could use a selected range or a cell matched to a record ID. The supplied version deliberately uses the cell under the picture’s upper-left corner so it can process a sheet without asking for a target cell for every picture.

Does this work with merged cells?
Yes. If the top-left cell belongs to a merged range, the macro uses the full merged area’s width and height. The picture is centered within that area, not just within the first cell of the merge.

Can I run this in Excel for the web?
No. Excel for the web does not execute VBA. Open the workbook in desktop Excel for Windows or Mac to run the macro; alternatively, use a supported in-cell picture feature or another web-compatible workflow.

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.