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
- 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.xlsxfile cannot store macros. - 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. - Insert a module. In the editor's Project window, right-click your workbook, then choose Insert > Module. A blank code window opens.
- Paste this basic macro:
Sub InsertPicture()Dim imgPath As StringimgPath = "C:\Images\photo.jpg"ActiveSheet.Shapes.AddPicture _FileName:=imgPath, _LinkToFile:=msoFalse, _SaveWithDocument:=msoTrue, _Left:=Range("B2").Left, _Top:=Range("B2").Top, _Width:=100, Height:=100End Sub
Change the path to a real image on your machine. - Run it. Click anywhere inside the macro and press
F5, or pressAlt + F8from the worksheet, select InsertPicture, and click Run. The picture appears at cell B2, sized 100 x 100 points. - (Optional) Let the user pick the file. Replace the hardcoded path with a file dialog:
Dim fd As FileDialogSet fd = Application.FileDialog(msoFileDialogFilePicker)fd.Filters.Clearfd.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. - (Optional) Fit the picture into a cell. After inserting, capture the shape in a variable and resize it to the cell:
Dim shp As ShapeSet shp = ActiveSheet.Shapes.AddPicture(...)shp.LockAspectRatio = msoFalseshp.Left = Range("B2").Leftshp.Top = Range("B2").Topshp.Width = Range("B2").Widthshp.Height = Range("B2").Height
Setting LockAspectRatio to False lets the image stretch to the exact cell size. - (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(usexlMoveto 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.Insertstill 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. UseShapes.AddPicturewithLinkToFile:=msoFalseto embed it properly. - Not saving as .xlsm. Macros in a
.xlsxfile 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()orApplication.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 = Falsebefore inserting many pictures and back toTrueafterwards; 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 imgPathand 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 + F11if 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.