How to Put a Photo in a VBA UserForm

VBA & macros · Updated November 2, 2026

How to Put a Photo in a VBA UserForm

To put a photo in an Excel VBA UserForm, add an Image control, then either set its Picture property in the form designer or load a file with VBA’s LoadPicture function. Embedding a picture is convenient when it should travel with the workbook; loading it in code is useful when the picture may change or users need to choose a file.

What You’ll Need

  • Excel for Microsoft 365 on Windows and a workbook saved as an Excel Macro-Enabled Workbook (.xlsm).
  • Access to the VBA editor and a UserForm. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon, then select Developer.
  • A photo file. For the most reliable results with VBA’s built-in LoadPicture, use a JPEG, BMP, or GIF image. PNG files are not supported consistently across VBA environments, so convert a PNG to JPEG or BMP if it will not load.

Excel for the web cannot run VBA UserForms. Excel for Mac has differences in its VBA and form-control support; check whether the Image control is available in your version before building a form that must work on Mac.

Step-by-Step

  1. Open the VBA editor and create a form. In desktop Excel for Windows, press Alt+F11, or choose Developer > Visual Basic. In the editor, choose Insert > UserForm. The form appears in the project, and the Toolbox should be visible. If it is not, choose View > Toolbox. Save a copy of the workbook before running any macro: VBA changes cannot be undone with Ctrl+Z.
  2. Add an Image control. In the Toolbox, select the Image control, then click and drag on the UserForm to draw a rectangular area for the photo. Select the new control and press F4 to show its Properties window if it is not already visible. Set its (Name) property to imgPhoto. A descriptive name makes the code easier to read and helps prevent errors later. Adjust the control’s size by dragging its handles or changing its Height and Width properties.
  3. Choose whether to embed or load the picture. For a fixed photo, select the Image control and find its Picture property in the Properties window. Click the property’s value, then use the browse button that appears to select the image. The picture is stored with the form in the workbook, so users do not need a separate image file at runtime. For a photo that changes, or one selected by a user, leave the Picture property empty and use the code in the next step instead. You can use either method, but loading a different photo in code replaces the picture currently shown.
  4. Set how the photo fits. In the Properties window, set PictureSizeMode to 3 - fmPictureSizeModeZoom, if that option is displayed. Zoom keeps the whole photo visible while maintaining its proportions; it may leave unused space around the image. The Stretch setting fills the control but can distort faces or objects, while Clip shows only the part of the image that fits. You can also set this property in VBA. Keep the control’s shape and the photo’s proportions in mind when laying out the form.
  5. Load a photo when the form opens (optional). If you chose runtime loading, open the UserForm’s code window by right-clicking the form in Project Explorer and choosing View Code. Put the following procedure in the UserForm’s code module, not in a standard module. Change the filename to match your image. This example expects the photo beside the saved workbook:
Private Sub UserForm_Initialize()
    Dim photoPath As String

    ' Change the filename to the name of your photo.
    photoPath = ThisWorkbook.Path & "\product-photo.jpg"

    On Error GoTo PictureError

    If Len(Dir$(photoPath)) = 0 Then
        MsgBox "Photo not found: " & photoPath, vbExclamation
        Exit Sub
    End If

    Me.imgPhoto.PictureSizeMode = fmPictureSizeModeZoom
    Me.imgPhoto.Picture = LoadPicture(photoPath)
    Exit Sub

PictureError:
    MsgBox "The photo could not be loaded. Check its path and format.", vbExclamation
End Sub

UserForm_Initialize runs as the form is initialized. ThisWorkbook.Path refers to the folder containing the workbook, and the backslash joins that folder to the filename on Windows. Save the workbook before testing this approach: if it has never been saved, its path is empty. If the image is in another folder, replace the path-building line with its full path, such as C:\Images\product-photo.jpg. The workbook and image must remain accessible at that location.

  1. Optionally let a user browse for a photo. Add a CommandButton to the form, set its (Name) to cmdBrowse, and set its Caption to Choose photo. Double-click the button to create its click event, then use this procedure. It displays a file picker and puts the selected image in imgPhoto:
Private Sub cmdBrowse_Click()
    Dim selectedFile As Variant

    selectedFile = Application.GetOpenFilename( _
        "Image Files (*.jpg;*.jpeg;*.bmp;*.gif),*.jpg;*.jpeg;*.bmp;*.gif", _
        , "Choose a photo")

    ' A Boolean result means the user cancelled the dialog.
    If VarType(selectedFile) = vbBoolean Then Exit Sub

    On Error GoTo PictureError
    Me.imgPhoto.PictureSizeMode = fmPictureSizeModeZoom
    Me.imgPhoto.Picture = LoadPicture(CStr(selectedFile))
    Exit Sub

PictureError:
    MsgBox "Excel could not load that photo. Try a JPEG or BMP file.", vbExclamation
End Sub

The file picker filters for common formats, but a file extension alone does not guarantee that a file is a valid image. The error handler displays a message if the selected file cannot be read. If you do not need a browse button, skip this procedure and use either the embedded picture or the initialization code.

  1. Test the form and save it correctly. In the VBA editor, select the UserForm and press F5 to display it, or run the procedure in your workbook that calls UserForm1.Show (replace UserForm1 with your form’s actual name). Check that the photo appears, fits the control, and remains visible after closing and reopening the form. Save the workbook as .xlsm so it can retain VBA code. When reopening it, enable macros only if you trust the workbook’s source.

Common Mistakes

  • Putting the code in the wrong module: UserForm_Initialize belongs in the form’s code module. A procedure with the same name in a standard module will not automatically run when the form opens.
  • Using a control name that does not match: If your Image control is named Image1, code that refers to Me.imgPhoto will fail. Either rename the control or update every reference in the code.
  • Assuming an external image is embedded: A path in VBA points to a separate file; it does not package that file into the workbook. Other users need the same accessible file, or you should embed a fixed photo through the Picture property.
  • Choosing a format that LoadPicture cannot read: A PNG may display in other Office features but fail in this VBA approach. Convert it to a supported format and test the converted file.
  • Stretching a photo without checking its proportions: Stretch can make the image look unnaturally wide or tall. Use Zoom when preserving the original proportions matters.

Tips

  • Resize and crop the image before adding it if you want a specific composition. A photo with a similar aspect ratio to the Image control generally needs less unused space when using Zoom.
  • For a workbook shared with colleagues, embedding a stable image avoids broken file paths. If you need replaceable photos, keep the files in a shared folder and use a predictable, documented path—or offer the browse button.
  • Keep the image at a sensible resolution. Very large photos can make the workbook heavier and slower to open; reducing image dimensions before embedding can help.
  • In the VBA editor on a Mac, the keyboard shortcut for opening the editor may be Option+F11, sometimes with the Fn key depending on keyboard settings. Menu names and form-control availability can differ from Windows.

Troubleshooting

The form opens, but the picture is blank: Confirm that the code is in the correct UserForm module, that the control is named imgPhoto, and that the form actually runs its initialization event. For a file loaded in code, check that the workbook has been saved, the filename and folder are correct, and the file is available to the person opening the workbook. The example’s “Photo not found” message indicates that the path does not point to an existing file.

Excel reports an error at LoadPicture: Try opening the image in an image editor and saving a fresh JPEG or BMP copy. Check that the file is not corrupt and that its extension matches its actual format. If the error occurs only on another computer, compare the file locations and permissions; a path on your computer may not exist on theirs.

The image is distorted or partly missing: Set PictureSizeMode to Zoom to preserve the whole image, or enlarge the Image control. Clip intentionally crops what is visible, so switch away from Clip if parts of the photo disappear. If Zoom leaves borders, adjust the control’s dimensions or crop the original photo to a closer aspect ratio.

The code says a member or control is not found: Check spelling and capitalization in the control’s (Name) property and in the code. Confirm that you inserted an Image control, not a Label or another control, and compile the project by choosing Debug > Compile VBAProject to reveal code errors.

Frequently Asked Questions

Can I add a photo without writing VBA code?
Yes. Add an Image control, then select a file through its Picture property in the Properties window. This is often the simplest choice for a photo that never changes.

Is the picture saved inside the workbook?
A picture selected through the Image control’s Picture property is stored with the form in the workbook. A picture loaded from a path at runtime remains a separate file, so the path must work whenever the form is used.

Can the user choose a different photo?
Yes. Add a CommandButton and use its click procedure with Application.GetOpenFilename and LoadPicture. The selected photo appears for that form session; the example does not save the chosen file into the workbook permanently.

Why does my PNG not appear?
VBA’s built-in LoadPicture does not handle PNG consistently. Convert the image to JPEG or BMP and try again. If you need PNG transparency, note that converting to JPEG removes transparency; BMP or another suitable workflow may be preferable.

Will this UserForm work in Excel for the web?
No. Excel for the web does not run VBA macros or display VBA UserForms. Open the macro-enabled workbook in a supported desktop version of Excel instead.

Can I use the same instructions on Mac?
The overall idea is similar, but Mac Excel’s VBA editor, file paths, and UserForm control support differ from Windows. Confirm that your Mac version provides the Image control and test the form there; do not assume a Windows path or control setup will transfer unchanged.

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.