Automatically Displaying Thumbnails of a Graphic File

Written by Allen Wyatt (last updated January 23, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


Ron has a worksheet that includes a column containing filenames such as "abcdef." This corresponds to a JPG file ("abcdef.jpg") in the same folder as the worksheet. Ron wonders if there is a way to make Excel automatically display, in the cell just to the right of the filename, a thumbnail of the JPG file.

If, by "automatic," you mean that the image appears whenever a filename is placed into the cell, that is possible, but it is not necessarily the best way to go. Why? Because working with graphics files can take a huge number of resources in your workbook, and as you add more and more filenames, the amount of data that needs to be shuttled around can be huge.

A better approach is to develop a macro that will look at whatever cells you have selected (those cells should contain the filenames) and then insert the thumbnails to the right of those selected cells. Here's an example:

Sub Insert_Pics_By_Filename()
    Dim Source As Range
    Dim c As Range
    Dim s As Shape
    Dim sThumbSize As Single
    Dim sPath As String
    Dim sHeight As Single
    Dim sLeft As Single
    Dim xTop As Single  ' xTop instead of sTop because sTop is a keyword
    Dim sTemp As String

    sThumbSize = 0.75 ' Picture will be 75% of row height
    Set Source = Selection
    sPath = ActiveWorkbook.Path & "\"

    ' Check the filenames in the cells, first
    For Each c In Source.Cells
        sTemp = c.Value
        If Not (sTemp Like "*.jpg") Then
            MsgBox "Can't work with the filename:" & vbCr & vbCr & sTemp & " in cell " & c.Address
            Exit Sub
        End If
    Next c

    ' Delete all existing pictures
    For Each s In ActiveSheet.Shapes
        If s.Type = msoLinkedPicture Then
        End If
    Next s

    ' Insert the pictures
    For Each c In Source.Cells
        With c
            sHeight = .RowHeight * sThumbSize
            sLeft = .Offset(0, 1).Left + 5   ' Put 5 pixels from cell left
            xTop = .Top + 5   ' Put 5 pixels from cell top
            sTemp = .Value

            With ActiveSheet.Pictures.Insert(sPath & sTemp)
                With .ShapeRange
                    .LockAspectRatio = msoTrue
                    .Height = sHeight
                End With
                .Left = sLeft
                .Top = xTop
                .Placement = 1
                .PrintObject = True
            End With
        End With
    Next c
End Sub

The macro creates a thumbnail that is 75% of the height of the row and inserts that thumbnail to the right of the selected cells. If one of the selected cells doesn't have ".jpg" as part of the filename, then it will stop and tell you that there is an error with the name.

The macro isn't terribly robust, in that you could easily crash it. For instance, if the row heights is quite small, then the thumbnails will be very small—and if they are too small, then there could be a fatal error in inserting the thumbnail. You should be aware, as well, that the macro also deletes all pictures in the worksheet before it inserts the thumbnails. That way you don't run the risk of getting duplicate images inserted. If you want the macro to delete just a portion of the images, then you'll need to do a good deal of reworking the macro to detect and affect just the subset of images desired.


If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9983) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...


Word Slow to Open Documents

If you've noticed a slowdown in Word when it is opening a document, you probably would like to speed up the operation. ...

Discover More

Quickly Clearing Array Contents

An array of variables is a powerful element of VBA programming. If you want to clear what is stored in an array, here's a ...

Discover More

Controlling Entry Order on Unprotected Cells

When you protect a worksheet, one of the benefits is that you can limit which cells can be used for data entry. How a ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Setting the Default Fill Color for a Shape to None

When you insert a shape into a worksheet, Excel does some preliminary formatting on that shape. You can subsequently make ...

Discover More

Capturing a Screen

A picture is worth a thousand words, but getting the picture, particularly a screen shot, into a workbook may seem ...

Discover More

Taking a Picture

Excel allows you to capture portions of your worksheet as a picture that you can then use in a variety of other ways. ...

Discover More

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2021-01-29 07:35:55

Barry Brookshire

Thank you Mr. Harold Druss!!!

This works PERFECTLY!!!

2021-01-28 06:32:10

Harold Druss

Replace "Set Source = Selection" with:
Dim lRow As Long
'Find the last non-blank cell in column A(1)
lRow = Cells(Rows.Count, 1).End(xlUp).Row
Set Source = Range("A2:A" & lRow)

2021-01-27 09:20:15

Barry Brookshire

I'm new to VBA & by no means an expert.

I see great potential in using this code for adding pics in our routine safety audits, but the auditors are not "Excel savvy".

How can I make the code determine the last filename is reached in the column and make that the range to use to add in the pics so that you don't have to "pre-select" the range of filenames before executing the code?

1,000,000,000,000 thanks in advance!!!

2021-01-23 04:58:23


Thank you very much Allen!
I will use the given solution and let you know if it worked for me

This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.