VBA & Macro Problems

Insert and Resize Multiple Photos in Excel with VBA

Anum MushtaqAnum Mushtaq Nov 14, 2026 869 views

Question details

Insert multiple photos into designated Excel cells, fitting and centering each photo without changing its proportions.

Insert and Resize Multiple Photos in Excel with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet has designated photo cells, potentially six per page, with other cells reserved for notes.
Observed behavior
Each selected photo cell should receive one proportionally resized and centered image, while unselected note cells remain blank.
Before you start

Use a desktop version of Excel that supports VBA, and set the photo cells to their intended row heights and column widths. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and enable macros only if you trust the workbook.

Solution 1Recommended

Use a VBA macro to fit one photo into each selected cell

Select the destination cells and corresponding image files to insert, proportionally resize, and center one photo per cell.

The macro processes the selected areas and cells in Excel's selection order, then assigns files in the order returned by the file picker. That order may not match the order in which you clicked the files, so check the results.

Select only unmerged, visible photo cells. Leave cells intended for notes out of the selection.

1
Open a VBA module

In desktop Excel, open Developer > Visual Basic, then choose Insert > Module. On Windows, you can also press Alt+F11 to open the editor.

2
Paste the macro

Paste this code into the module: Sub InsertPhotosInSelectedCells() Dim targets As New Collection Dim area As Range, cell As Range Dim ws As Worksheet Dim picker As Object Dim pic As Shape Dim i As Long If TypeName(Selection) <> "Range" Then MsgBox "Select the destination cells first." Exit Sub End If Set ws = ActiveSheet For Each area In Selection.Areas For Each cell In area.Cells If cell.MergeCells Or cell.Width <= 0 Or cell.Height <= 0 Then MsgBox "Select only visible, unmerged cells." Exit Sub End If targets.Add cell Next cell Next area Set picker = Application.FileDialog(3) With picker .Title = "Select one photo for each selected cell" .AllowMultiSelect = True .Filters.Clear .Filters.Add "Images", "*.jpg;*.jpeg;*.png;*.bmp;*.gif" If .Show <> -1 Then Exit Sub If .SelectedItems.Count <> targets.Count Then MsgBox "Select exactly " & targets.Count & " photo(s)." Exit Sub End If On Error GoTo InsertError For i = 1 To targets.Count Set cell = targets(i) Set pic = ws.Shapes.AddPicture(.SelectedItems(i), False, True, _ cell.Left, cell.Top, -1, -1) pic.LockAspectRatio = True If pic.Width / pic.Height > cell.Width / cell.Height Then pic.Width = cell.Width Else pic.Height = cell.Height End If pic.Left = cell.Left + (cell.Width - pic.Width) / 2 pic.Top = cell.Top + (cell.Height - pic.Height) / 2 pic.Placement = xlMove Next i End With Exit Sub InsertError: MsgBox "Could not insert all photos: " & Err.Description End Sub

3
Select the photo cells

Return to the worksheet and select the destination cells, including nonadjacent cells if needed. For a six-photo page, select its six photo cells but not the cells reserved for notes.

4
Run the macro

Open Developer > Macros, select InsertPhotosInSelectedCells, and choose Run. In the file picker, select exactly one image file for each selected cell and confirm.

5
Check the layout

Inspect the worksheet to verify the photo order and use File > Print > Print Preview to check the page layout. If an insertion error occurs, remove any partially inserted photos before trying again.

Use a VBA macro to fit one photo into each selected cell
Changing cell dimensions: The pictures move with their cells but do not automatically resize when row heights or column widths change. Set cell dimensions before running the macro.
Free Microsoft Office alternative

Manage photo worksheets with WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar interface for working with spreadsheets and inserting images. Its Excel format compatibility makes it easy to move existing workbooks, although it does not fix or guarantee compatibility with this Excel VBA macro.

Insert and arrange photos in spreadsheet worksheetsOpen and edit common Excel formats, including XLSX and XLSUse a familiar spreadsheet interface for an easier transitionWork with a free, lightweight Office suite
microsoft office alternative - wps office

Frequently Asked Questions

Can I leave blank cells between the photos for notes?

Yes. Select only the destination photo cells before running the macro. The macro does not insert pictures into unselected note cells.

Why does the macro ask for the same number of photos as selected cells?

It assigns one file to each selected cell. If the counts differ, it stops before inserting pictures so you can select the correct number of files.

Will the photos retain their original proportions?

Yes. The macro locks each picture's aspect ratio and fits it within the cell's width and height. It then centers the picture, so some space may remain around it.

Can I use merged cells as photo placeholders?

Not with this macro: it stops if a selected cell is merged. Unmerge the destination cells and adjust their row heights and column widths before running it.

Why are my photos in a different order than expected?

Excel processes selected areas and cells in selection order, while the file picker supplies its own file order. Check the inserted pictures and rearrange them if necessary.