Build a Data Entry Form in Excel VBA (UserForm Step by Step)

Build a Data Entry Form in Excel VBA (UserForm Step by Step) featured illustration

πŸ“Ž This article includes 2 downloadable practice files ↓

⏱ 3 min read

When several people type into the same sheet, rows get skipped, dates get typed as text and names get misspelt. A UserForm gives them a clean form with drop-downs and checks, and writes each entry into a Table in the right format.

In this article
  1. 1. The table
  2. 2. Design the form
  3. 3. Fill drop-downs when the form opens
  4. 4. Validate and save
  5. 5. Open it from a button
  6. Where people go wrong
  7. Continue learning

1. The table

On a sheet called Entries, create a Table (Ctrl+T) named tblEntries with columns Date, Customer, Region, Amount, EnteredBy. Keep drop-down lists (Regions, Customers) on a Lists sheet.

Advertisement

2. Design the form

Alt+F11 β€Ί Insert β€Ί UserForm. From the Toolbox add labels and: txtDate (TextBox), cboCustomer and cboRegion (ComboBox), txtAmount (TextBox), cmdSave and cmdClose (CommandButton). Set each control’s Name in the Properties window.

3. Fill drop-downs when the form opens

Private Sub UserForm_Initialize()
    cboRegion.List = Application.Transpose(Sheets("Lists").Range("A2:A5").Value)
    cboCustomer.List = Application.Transpose(Sheets("Lists").Range("C2:C50").Value)
    txtDate.Value = Format(Date, "dd-mm-yyyy")
End Sub

4. Validate and save

Private Sub cmdSave_Click()
    If Not IsDate(txtDate.Value) Then MsgBox "Enter a valid date": txtDate.SetFocus: Exit Sub
    If cboCustomer.ListIndex = -1 Then MsgBox "Pick a customer from the list": Exit Sub
    If Not IsNumeric(txtAmount.Value) Or Val(txtAmount.Value) <= 0 Then MsgBox "Amount must be a positive number": Exit Sub
    Dim r As ListRow
    Set r = Sheets("Entries").ListObjects("tblEntries").ListRows.Add
    r.Range.Value = Array(CDate(txtDate.Value), cboCustomer.Value, cboRegion.Value, CDbl(txtAmount.Value), Environ("Username"))
    txtAmount.Value = "": txtAmount.SetFocus      ' ready for the next entry
End Sub
Private Sub cmdClose_Click(): Unload Me: End Sub
πŸ’‘ Set the ComboBoxes’ Style property to fmStyleDropDownList so users can only pick from the list, never type a misspelt name.

5. Open it from a button

In a normal module: Sub ShowForm(): frmEntry.Show: End Sub, then Insert β€Ί Shapes on the sheet, right-click β€Ί Assign Macro. Save as .xlsm (or .xlsb).

Where people go wrong

Problem Fix
Dates stored as text Convert with CDate before writing
Amounts as text CDbl before writing
Rows overwrite each other Use ListRows.Add, not a fixed row number

Full course: Excel VBA course, lesson 15 on UserForms.

⚑ Interactive Practice Lab & Formula Tester:

Test formulas dynamically in our web formula laboratory or download the companion Excel workbook.

πŸ“Ž Practice files for this article

⬇ Download all 2 files (ZIP Β· 7 KB)

  • πŸ“—
    Data-entry starter workbookAn Entries table, dropdown lists and step-by-step setup for the UserForm.
    ⬇ XLSX Β· 7 KB
  • πŸ“„
    userform-code.txtComplete UserForm code with input validation that adds rows to the table.
    ⬇ TXT Β· 1 KB

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong