Yes, you can insert a picture from a folder into Excel using VBA, and it takes just a few lines of code. The core idea is to use the Shapes.AddPicture method, pointing it at a file path on your computer or network. Once the macro runs, the image appears on the worksheet, sized and positioned however you specify. This is handy for product catalogues, employee photo lists, or any sheet where you need images pulled in automatically.

What You'll Need

  • Excel for Windows (the VBA picture insertion methods covered here are reliable on desktop Windows; Excel for Mac handles pictures differently and some shapes methods behave inconsistently there)
  • A workbook saved as .xlsm (macro-enabled), or a willingness to save it that way
  • Image files in a known folder (JPG, PNG, GIF and BMP all work)
  • Macros enabled in your security settings when you run the code

Step-by-Step

  1. Save your workbook as macro-enabled. Go to File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm) from the file type list, and save. A plain .xlsx file cannot store macros.
  2. Open the VBA editor. Press Alt + F11, or go to the Developer tab and click Visual Basic. If you don't see a Developer tab, enable it via File > Options > Customize Ribbon and tick Developer in the right-hand list. Exact labels can vary slightly between Excel versions.
  3. Insert a module. In the editor's Project window, right-click your workbook, then choose Insert > Module. A blank code window opens.
  4. Paste this basic macro:
    Sub InsertPicture()
        Dim imgPath As String
        imgPath = "C:\Images\photo.jpg"
        ActiveSheet.Shapes.AddPicture _
            FileName:=imgPath, _
            LinkToFile:=msoFalse, _
            SaveWithDocument:=msoTrue, _
            Left:=Range("B2").Left, _
            Top:=Range("B2").Top, _
            Width:=100, Height:=100
    End Sub
    Change the path to a real image on your machine.
  5. Run it. Click anywhere inside the macro and press F5, or press Alt + F8 from the worksheet, select InsertPicture, and click Run. The picture appears at cell B2, sized 100 x 100 points.
  6. (Optional) Let the user pick the file. Replace the hardcoded path with a file dialog:
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.Filters.Clear
    fd.Filters.Add "Images", "*.jpg; *.png; *.gif; *.bmp"
    If fd.Show = True Then imgPath = fd.SelectedItems(1)
    This opens a standard picker so you can browse to any folder.
  7. (Optional) Fit the picture into a cell. After inserting, capture the shape in a variable and resize it to the cell:
    Dim shp As Shape
    Set shp = ActiveSheet.Shapes.AddPicture(...)
    shp.LockAspectRatio = msoFalse
    shp.Left = Range("B2").Left
    shp.Top = Range("B2").Top
    shp.Width = Range("B2").Width
    shp.Height = Range("B2").Height
    Setting LockAspectRatio to False lets the image stretch to the exact cell size.
  8. (Optional) Anchor the picture to the cell. By default, pictures are positioned absolutely on the sheet. To make a picture move and resize with its cell, set shp.Placement = xlMoveAndSize (use xlMove to move without resizing).

Common Mistakes

  • Wrong path syntax. The path must be a complete, valid Windows path. A missing backslash, a typo in the folder name, or pointing to OneDrive's local placeholder instead of a synced file will throw a runtime error.
  • Using the old Pictures.Insert method. ActiveSheet.Pictures.Insert still works but in modern Excel it links the image to the file rather than embedding it, so the picture can break when the file is moved. Use Shapes.AddPicture with LinkToFile:=msoFalse to embed it properly.
  • Not saving as .xlsm. Macros in a .xlsx file are silently discarded when you close the workbook.
  • Forgetting SaveWithDocument:=msoTrue. Without it the image may not survive saving and reopening.
  • Sizing in the wrong units. Width and Height are in points, not pixels. Assuming pixels leads to pictures that look too small or too large.

Tips

  • Build the path with a variable folder so you can loop: imgPath = folder & "\" & cellValue & ".jpg", looping down a list of names in column A and inserting each picture next to its name. This is the classic employee-photo or product-image pattern.
  • Use Dir() or Application.FileDialog(msoFileDialogFolderPicker) to grab a whole folder of images at once instead of one file.
  • Check the file exists before inserting: If Dir(imgPath) = "" Then MsgBox "File not found" prevents ugly runtime errors mid-loop.
  • Set Application.ScreenUpdating = False before inserting many pictures and back to True afterwards; it makes bulk inserts dramatically faster.
  • Delete a picture in code with shp.Delete, handy when refreshing images that get replaced.

Troubleshooting

  • Run-time error 1004 on AddPicture: nearly always a bad path or a missing file. Print the path with Debug.Print imgPath and paste it into File Explorer to confirm it opens. Also check for straight quotes around the path in code, not smart quotes copied from a document.
  • Nothing happens when you run the macro: confirm macros are enabled (a yellow security bar appears under the ribbon when they're blocked). Close and reopen the workbook, then click Enable Content.
  • Alt + F11 does nothing: the Developer tab missing is normal, but the shortcut should always work on Windows. On some keyboards you need Alt + Fn + F11 if function keys are media keys by default.
  • The macro disappears after saving: you saved as .xlsx. Save again as .xlsm.
  • Pictures insert in the wrong place or pile up: you're using fixed Left/Top values in a loop. Base the position on a cell that changes with the loop, e.g. Cells(r, 2).Left.
  • Excel for Mac behaves differently: the Shapes object model and file paths (which use forward-slash style POSIX paths via AppleScript sandboxing) differ on Mac. If you're on Mac, test carefully or run the macro on Windows.

Frequently Asked Questions

Can VBA insert multiple pictures at once?

Yes. Loop through either a range of file names or the contents of a folder using Dir(), calling AddPicture for each file and offsetting the Top or Left position each time so the images don't stack.

How do I make the picture exactly fit a cell?

Set the shape's Left, Top, Width and Height to the target cell's Left, Top, Width and Height properties, and set LockAspectRatio = msoFalse first so the image stretches instead of keeping its ratio.

Will the picture stay in the file if I email the workbook?

Yes, as long as you inserted it with LinkToFile:=msoFalse and SaveWithDocument:=msoTrue. The image is embedded in the workbook, not tied to the folder on your PC.

Can I insert a picture without VBA, just manually?

Of course: Insert > Pictures > This Device lets you browse to any folder. VBA is worth it when you need to repeat the task, insert many images, or position them automatically.

Does this work in Excel Online?

No. Excel for the web does not support running VBA macros. You can insert pictures manually there, but any macro must be run in the desktop version.