Excel VBA Data Entry Form With a Picture Box: How It Works

VBA & macros · Updated November 2, 2026

Excel VBA Data Entry Form With a Picture Box: How It Works

Yes. In Excel for Microsoft 365 on Windows, make a VBA UserForm, add an Image control to act as the picture box, and use a file picker to preview a selected image. The form can then write the entered details and the image file’s path to a worksheet; the example below shows the setup and code.

What “picture box” means in an Excel form

Excel’s VBA UserForms do not use a control literally named “PictureBox.” The comparable control is called Image. It displays a picture inside the form, and VBA can load a selected image into its Picture property. You can resize the control and set its PictureSizeMode property to zoom the image within the available area.

A useful form usually combines that preview with ordinary controls: text boxes for information, command buttons for actions, and labels that tell people what to enter. The Image control is a preview area, not a database field. In the example here, the worksheet stores the person’s name, department, and the image’s file path. This distinction matters: saving a path does not put the picture itself inside the workbook.

Choose what the form should save

Before laying out controls, decide whether the workbook needs to retain an image or merely display it while someone enters a record. Saving a path is simpler and keeps the workbook smaller, but the file must remain at that location. If the image is moved, renamed, or unavailable to another computer, the stored path will no longer point to it.

For a basic contact or inventory form, a clear worksheet structure is enough:

  • Column A: Name
  • Column B: Department
  • Column C: Photo path

Put those headings in row 1 of a worksheet named Records. Each submission goes into the next available row. Use a consistent, shared folder for image files if other workbook users need to open them. For a portable, self-contained workbook, images can instead be embedded as worksheet pictures, but that requires extra code and increases workbook size.

Create the UserForm and controls

In desktop Excel for Windows, save a copy of the workbook as an Excel Macro-Enabled Workbook (.xlsm) before adding VBA. Press Alt+F11 to open the Visual Basic Editor, then choose Insert > UserForm. In the Properties window, set the form’s (Name) to frmEntry. The form name is what the launch macro will use.

Use the Toolbox to add the controls below. Set each control’s (Name) property exactly as shown; the code refers to these names. Add labels with readable captions such as Name, Department, and Photo.

  • Two TextBox controls: txtName and txtDepartment.
  • One Image control: imgPhoto. Give it a clear rectangular area for the preview.
  • Two CommandButton controls: cmdBrowse and cmdSave. Set their captions to Browse… and Save.

The UserForm designer is accessed through the desktop Visual Basic Editor, not Excel for the web. Excel for the web cannot run VBA macros or show VBA UserForms. Mac users should check the controls and file-picker behavior in their installed desktop Excel version; the steps and code below target Windows, where the Office file picker used here is available.

Add the image picker and save code

Double-click the UserForm to open its code window and add the following event procedures. They assume the controls and worksheet are named exactly as described. The Browse button opens a single-file picker, loads the selected picture into the Image control, and remembers its path. Save validates the entries and writes one row.

Option Explicit

Private mPhotoPath As String

Private Sub UserForm_Initialize()
    ' Start with an empty preview and a fitted image.
    mPhotoPath = vbNullString
    Me.imgPhoto.PictureSizeMode = fmPictureSizeModeZoom
    Set Me.imgPhoto.Picture = LoadPicture()
End Sub

Private Sub cmdBrowse_Click()
    Dim selectedPath As String

    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Choose a photo"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Picture files", "*.jpg;*.jpeg;*.bmp;*.gif;*.ico"
        .Filters.Add "All files", "*.*"
        If .Show <> -1 Then Exit Sub
        selectedPath = .SelectedItems(1)
    End With

    On Error GoTo LoadError
    Set Me.imgPhoto.Picture = LoadPicture(selectedPath)
    mPhotoPath = selectedPath
    Exit Sub

LoadError:
    mPhotoPath = vbNullString
    Set Me.imgPhoto.Picture = LoadPicture()
    MsgBox "Excel could not load that picture. Choose a supported image file.", vbExclamation
End Sub

Private Sub cmdSave_Click()
    Dim ws As Worksheet
    Dim nextRow As Long

    If Len(Trim$(Me.txtName.Value)) = 0 Then
        MsgBox "Enter a name before saving.", vbExclamation
        Me.txtName.SetFocus
        Exit Sub
    End If
    If Len(Trim$(Me.txtDepartment.Value)) = 0 Then
        MsgBox "Enter a department before saving.", vbExclamation
        Me.txtDepartment.SetFocus
        Exit Sub
    End If
    If Len(mPhotoPath) = 0 Then
        MsgBox "Choose a photo before saving.", vbExclamation
        Exit Sub
    End If

    Set ws = ThisWorkbook.Worksheets("Records")
    nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
    If nextRow < 2 Then nextRow = 2

    ws.Cells(nextRow, "A").Value = Trim$(Me.txtName.Value)
    ws.Cells(nextRow, "B").Value = Trim$(Me.txtDepartment.Value)
    ws.Cells(nextRow, "C").Value = mPhotoPath

    MsgBox "Record saved in row " & nextRow & ".", vbInformation
    Me.txtName.Value = vbNullString
    Me.txtDepartment.Value = vbNullString
    mPhotoPath = vbNullString
    Set Me.imgPhoto.Picture = LoadPicture()
End Sub

In the code window, VBA uses the ampersand operator to join the row number into the confirmation message. The HTML above displays that operator as &; in the Visual Basic Editor, enter an ordinary & character. Likewise, enter the comparison operator as <> in VBA, not as the HTML text shown in the listing. These character substitutions are needed only to display the code safely in an HTML article.

To show the form, choose Insert > Module in the Visual Basic Editor and add this separate procedure. Change the form name if you used a different one.

Option Explicit

Public Sub ShowEntryForm()
    ' Display the data-entry window.
    frmEntry.Show
End Sub

Return to Excel and run ShowEntryForm from the Macros dialog, or assign it to a worksheet button. On Windows, Alt+F8 opens the Macros dialog; on Mac, the shortcut differs by keyboard and Excel version, so use Tools > Macro > Macros if needed. Save a backup copy before running a macro: VBA changes cannot be reversed with Ctrl+Z.

How the record and preview work

The private variable mPhotoPath holds the selected file path while the form is open. The Image control displays the picture, while the save procedure writes that path into column C. Keeping the path in a variable prevents the save routine from needing to read a filename back from the displayed image, which is a picture object rather than a text field.

The next-row calculation looks down column A and starts after its last non-empty cell. With headings in row 1, the first entry is placed in row 2. If column A is empty, the calculation still resolves to row 2 because the code enforces that minimum. Keep column A populated for every record; if users delete names but leave other data behind, the form may reuse a row that appears empty in column A.

The Zoom setting preserves the image’s proportions while fitting it inside the control, so a portrait photo is not stretched to fill a landscape box. It does not improve a low-resolution image. For a neat form, make the Image control large enough to inspect the photo and keep the Browse and Save buttons separate so users do not mistake choosing a picture for submitting a record.

Supported image files and common problems

VBA’s LoadPicture function is commonly used with formats such as JPEG, BMP, GIF, and ICO. The picker filter in the example lists those formats. PNG loading can vary with the Office/VBA environment and the image control, so do not promise PNG support just because a file picker can display PNG files. If a PNG fails, convert it to a supported format or test the required format in the target desktop Excel installation.

  • The form will not open: confirm that macros are enabled for a trusted workbook and that the form is named frmEntry.
  • “Subscript out of range” appears: check that the worksheet tab is named Records. The name in the VBA code must match the tab name.
  • The image preview is blank: choose another supported file and make sure it can be accessed. A damaged file or unsupported image format may cause LoadPicture to fail.
  • The picture cannot be found later: the worksheet contains a path, not an embedded copy. Restore the file at that path or update the stored path.
  • A new record overwrites data: verify that column A has a name in every existing record and that the headers are in row 1.

Keep the image inside the workbook instead

If a workbook must travel without a separate image folder, storing a path alone is not enough. A common alternative is to insert the selected picture onto the worksheet as a Shape and position it beside the record, perhaps with a consistent thumbnail size. That approach embeds a copy in the workbook, but the save code must also add the picture, size and position it, and associate it with the correct row. It also needs a policy for deleting or replacing a picture when a record changes.

Embedding images increases workbook file size, especially when full-resolution photos are used. A practical compromise is to resize or compress source images before adding them and use small thumbnails for browsing. If the workbook is intended as a lightweight index, store paths and keep the image files in a managed shared folder instead. Choose based on portability, file size, and how the workbook will be shared, rather than assuming one method is always better.

Security and maintenance

Macros can change workbook contents, so only enable VBA in workbooks from sources you trust. A form is not a security boundary: people with access to the workbook may be able to inspect its data or alter its code, depending on workbook permissions and environment. Avoid storing sensitive photos or personal information in a broadly shared workbook unless its storage and access arrangements are appropriate.

For a maintained workbook, add clear instructions on the form, test with a copy of the data, and decide how duplicate submissions should be handled. The example intentionally does not enforce unique names because two people can share a name. A stronger design might use an employee or product ID, check it before saving, and record who submitted an entry. Those rules depend on the data, so they should be designed rather than guessed in generic code.

Frequently Asked Questions

Can I add a picture box to an Excel VBA UserForm?
Yes. Add an Image control from the UserForm Toolbox and load a picture into its Picture property with VBA. “Image” is the control’s name in the Toolbox; “picture box” is a common informal description.

Does this form save the picture in the worksheet?
No. The example previews the picture in the form and saves its file path in column C. To keep the picture in the workbook, add code that inserts the image as a worksheet Shape or use another storage design.

How do I show the form when the workbook opens?
You can call frmEntry.Show from the Workbook_Open event in the ThisWorkbook code module. Consider whether users should be able to open the workbook without the form, and test the event with macros enabled before distributing it.

Can I use the VBA picture form in Excel for the web?
No. Excel for the web does not run VBA macros or display VBA UserForms. Use desktop Excel for this design, or choose a web-compatible form or application if browser-based entry is required.

Why does my PNG not appear in the Image control?
LoadPicture does not reliably load PNG files in every relevant VBA/Office environment. Try a JPEG or BMP, or test a PNG-specific approach in the exact desktop Excel version and operating system used by the workbook’s users.

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.