To show a picture in an Excel VBA UserForm, add an Image control and either set its Picture property in the designer or load a file into it with VBA’s LoadPicture function. For a picture that should travel with the workbook, embed it in the form; for one that can change, load it from a known file path when the form opens.
What an Image control does
A UserForm is a custom dialog built in the Visual Basic Editor, and an Image control is the control intended to display a graphic on it. The control can show a picture within a fixed area of the form. It does not create a separate image window, and it is not the same as placing a picture on an Excel worksheet.
In Excel for Microsoft 365 on Windows, open the editor with Alt+F11. In the Project Explorer, select the workbook, then choose Insert > UserForm. If the Toolbox is not visible, choose View > Toolbox. Click the Image control in the Toolbox and draw a rectangle on the form. In the Properties window, change its (Name) value to a useful name such as imgPhoto. The name matters: code that refers to Me.imgPhoto must match the control’s actual name.
The same control can display a company logo, product photo, chart image, or small illustration. It is a display control, not an image editor: users cannot crop, rotate, or annotate the picture through it. Plan the image area and prepare the graphic before adding it.
Embed a picture in the UserForm
Embedding is the straightforward choice when the image is fixed and the workbook should remain self-contained. Select the Image control, find the Picture property in the Properties window, click its value or the ellipsis button, and choose a supported image file. The selected image is stored as part of the UserForm design, so the form does not need to find that original file each time it opens.
Use this approach for a logo or other artwork that should look the same for every workbook user. It avoids a common deployment problem: a hard-coded path such as C:\Users\Alex\Pictures\logo.jpg may work on one computer but not on another. The trade-off is that replacing the picture means editing the form or writing code; a user cannot simply replace a separate file and expect the embedded image to update.
After adding the graphic, set the Image control’s PictureSizeMode property. Its choices determine whether the picture is clipped, stretched, or scaled to fit. The sizing options are described below; they affect appearance, not whether the image is embedded.
Load a picture when the form opens
Use LoadPicture when the picture is selected or updated outside the form, or when different users need different images. Add this code to the UserForm’s code module. In this example, the workbook and product.jpg are in the same folder, and the Image control is named imgPhoto.
Private Sub UserForm_Initialize()
Dim imagePath As String
On Error GoTo PictureError
' Require a saved workbook so its folder is available.
If Len(ThisWorkbook.Path) = 0 Then
MsgBox "Save the workbook before loading its picture.", vbExclamation
Exit Sub
End If
' Build a path to product.jpg beside the workbook.
imagePath = ThisWorkbook.Path & Application.PathSeparator & "product.jpg"
If Len(Dir$(imagePath)) = 0 Then
MsgBox "Picture not found: " & imagePath, vbExclamation
Exit Sub
End If
Me.imgPhoto.PictureSizeMode = fmPictureSizeModeZoom
Me.imgPhoto.Picture = LoadPicture(imagePath)
Exit Sub
PictureError:
MsgBox "Excel could not load the picture: " & Err.Description, vbExclamation
End Sub
UserForm_Initialize runs when the form is initialized, before it is shown. The code checks for an unsaved workbook and a missing file, then loads the picture and scales it proportionally. Replace product.jpg with the actual filename and extension. Before running any macro, save a copy of the workbook: VBA changes cannot be undone with Ctrl+Z.
To display the form, code elsewhere in the workbook can call its name followed by .Show, for example frmPhoto.Show if the form is named frmPhoto. You can run that line from a procedure or call it from a worksheet button. The form’s name and the Image control’s name are separate properties, so check both if VBA reports that it cannot find a member.
Choose a reliable file path
A path is a frequent source of trouble because LoadPicture needs to locate a real file on the computer running Excel. A full path is simple but tied to one machine. A path built from ThisWorkbook.Path is more portable if the image stays beside the workbook and that folder is distributed with it. If the workbook is moved without the image, the relative arrangement no longer works.
For a file in a subfolder called Images, build the path by adding the folder name and filename to the workbook folder. Keep the subfolder name and spelling consistent, and avoid assuming that every user has the same Windows account or Documents folder. Network paths can work too, but the user must have access to the shared location and the network must be available when the form loads.
Another useful pattern is to let the user choose a file. This button-event example assumes that the form contains an Image control named imgPhoto and a CommandButton named cmdBrowse. The file dialog returns False when the user cancels.
Private Sub cmdBrowse_Click()
Dim selectedFile As Variant
On Error GoTo PictureError
selectedFile = Application.GetOpenFilename( _
FileFilter:="Image files (*.bmp;*.gif;*.jpg;*.ico;*.wmf;*.emf),*.bmp;*.gif;*.jpg;*.ico;*.wmf;*.emf", _
Title:="Choose a picture")
If VarType(selectedFile) = vbBoolean Then Exit Sub
Me.imgPhoto.PictureSizeMode = fmPictureSizeModeZoom
Me.imgPhoto.Picture = LoadPicture(CStr(selectedFile))
Exit Sub
PictureError:
MsgBox "Excel could not load the selected picture: " & Err.Description, vbExclamation
End Sub
This gives the user a file picker, but it does not make the selected picture part of the workbook. The chosen file still needs to be available again if the form is expected to load it later. The dialog filter is a convenience, not a guarantee that every file with a listed extension contains a valid picture.
Fit, stretch, or crop the image
The Image control’s PictureSizeMode property controls how a picture occupies the control’s rectangle. In code, the constants most useful for ordinary display are:
fmPictureSizeModeClipkeeps the image at its original size and clips parts that fall outside the control.fmPictureSizeModeStretchfills the control, but can distort the picture if the control and image have different proportions.fmPictureSizeModeZoomscales the full image to fit while keeping its proportions. This is usually the safest choice for photos and logos.
For example, a wide photograph in a square control may leave unused space above and below when Zoom is selected. That is expected: preserving the whole picture without distortion requires empty space when the aspect ratios differ. Stretch fills the square but makes the photograph look compressed or elongated. If you want a deliberate close-up crop, prepare a cropped source image or design the layout around clipping rather than expecting Zoom to fill every edge.
Set the control’s Width and Height in the Properties window, or adjust them as part of the form layout. Leave enough room for the picture at the form’s typical display size, and test the form at the size users will see. Resizing a UserForm does not automatically guarantee that every control or image will scale in the way you intend.
Supported formats and image quality
The VBA LoadPicture function is commonly used with formats such as BMP, GIF, JPG/JPEG, ICO, WMF, and EMF. The available behavior can depend on the Office and Windows components in use, so use a format that your target Excel installation can load. PNG is a common source of confusion: it is not consistently supported by the classic MSForms loading path. If a PNG fails, convert it to JPG for a photograph or BMP for a simple graphic, then try again.
Use a source image large enough for its intended on-screen size, but do not assume that a very large photo is necessary for a small UserForm preview. Large embedded pictures can increase workbook size, while a large external file can take longer to load and may consume more memory. If file size matters, resize or compress a copy of the image before embedding or distributing it. Keep an original separately if you may need to edit it later.
JPG is usually suitable for photographs, while GIF can be useful for simple graphics. Do not rely on the UserForm Image control as an animated-image player; animation support should not be assumed just because the file extension is GIF. The control’s purpose is to display a picture, and exact rendering can vary with the image format and Office environment.
Common problems and how to fix them
- The form opens but the picture is blank: check that the control name in code matches the name shown in Properties, that the file exists at the constructed path, and that the code is in the correct UserForm module.
- Run-time error 481, “Invalid picture”: the file may be unsupported, damaged, or have an extension that does not match its actual contents. Try opening it in an image editor and saving a fresh JPG or BMP copy.
- The image appears stretched: set
PictureSizeModetofmPictureSizeModeZoomrather than Stretch, or change the control’s proportions to better match the source image. - The image works on one computer only: replace a user-specific full path with an embedded picture or a path relative to the workbook, and distribute any required image file alongside it.
- The form cannot find the image after saving: saving the workbook does not copy an external file into the workbook folder. Place the image there yourself, or embed it in the designer.
Also check whether the workbook has been saved before building a relative path. An unsaved workbook has no normal folder path for this purpose. If an error appears only when a particular file is selected, test with a small, known-good JPG to distinguish a path problem from a format or file problem.
Embedded picture or external file?
Choose an embedded image when reliability and portability matter more than changing the picture without editing the workbook. It is a good fit for a fixed logo, help illustration, or product image that should always accompany that particular form. Once embedded, users do not need access to the original source file just to see the picture.
Choose an external file when the image is expected to change, is selected by the user, or should be maintained separately from the workbook. This is useful for a photo viewer or a form that displays a different product image based on a selection. It makes file management part of the design: document where images belong, decide how missing files should be reported, and avoid hard-coding paths that only exist on the developer’s computer.
There is no special charge for using the Image control itself; it is included with the VBA UserForm tools in desktop Excel for Windows. The practical costs are time spent preparing images and maintaining files, plus any workbook-size increase from embedded graphics. Excel for the web does not run VBA UserForms, so this approach is for desktop Excel. Excel for Mac has VBA support, but Windows-specific paths and differences in Office components can affect code and graphics behavior; verify the form on the Mac versions your users need to support.
Frequently Asked Questions
How do I add an Image control to a VBA UserForm?
Open the form in the Visual Basic Editor, show the Toolbox if needed, select Image, and draw the control on the form. Rename it in the Properties window, then set its Picture property or assign a picture in code.
How do I load a picture from a cell value or filename?
Use the cell’s text to build a valid file path, then pass that path to LoadPicture and assign the result to the Image control’s Picture property. Check that the file exists first; a filename stored in a cell does not itself contain the image.
Can I show a picture stored in an Excel worksheet cell?
A cell can contain a file path or other text, but it does not normally hold a picture that LoadPicture can read directly. If the picture is embedded on a worksheet, export or save it as a supported image file, or use a different design that displays the worksheet picture instead of loading it into a UserForm.
Why does my PNG not display in the UserForm?
The classic MSForms LoadPicture route does not reliably support PNG files across Excel installations. Convert the image to a supported format such as JPG or BMP and test that file. Changing only the extension does not convert the image format.
Can a UserForm show a picture without an external file?
Yes. Set the Image control’s Picture property in the UserForm designer to embed the graphic in the workbook. That is generally the simplest option for a fixed image that must travel with the form.
Related
- Multiply Hours by Money in Excel: A Practical Guide
- How to Multiply a List of Numbers in Excel: 10 Methods
- How to Multiply Hours by Rate in Excel and Format Totals
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Copy and Paste Range in ExcelTo copy an Excel range with VBA, use sourceRange.Copy Destination:=destinationCell; Excel copies the cells to the destination, including…
- Excel VBA Not Working? Diagnose Common Macro ProblemsIf Excel VBA is not working, first check that you are using the desktop version of Excel, the…
- Insert a Table in Excel Using VBA: Code and TipsIn Excel for Microsoft 365 on Windows, create a table with VBA by specifying the range that contains…
- VBA Macros in Google Sheets: What Works InsteadNo. Google Sheets cannot run VBA macros: VBA is Microsoft Excel’s programming language, and Sheets uses Google Apps…
- Can Google Sheets Run Excel Macros? Key DifferencesNo. Google Sheets cannot run Excel VBA macros. It has its own automation tools—Google Apps Script and a…
- Vba loop through rows?Learn how to loop through Excel rows with VBA, find the last row, handle conditions and deletions, and…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.