An Excel VBA UserForm is a custom window for collecting information or giving users a guided way to run workbook tasks. In desktop Excel for Windows, you create it in the Visual Basic Editor, add controls such as text boxes and buttons, then write VBA event procedures that respond to user actions.
What an Excel VBA UserForm Is
A UserForm is a small, customizable interface that belongs to an Excel VBA project. It can appear over a workbook and present fields, choices, instructions, and buttons without asking the user to work directly in cells. For example, a workbook might use a form to record a customer’s name, department, and request, then place the submitted information in the next available row of a worksheet.
A form is made from a UserForm object and controls. Common controls include:
- Label: displays explanatory text, such as “Employee name.”
- TextBox: lets someone type a value.
- ComboBox: provides a drop-down list, and may also allow typing.
- ListBox: displays one or more selectable items.
- CommandButton: runs an action, such as Save or Cancel.
- CheckBox and OptionButton: collect yes/no choices or selections from a group.
Excel UserForms are not the same as worksheet controls. A worksheet control sits on a sheet; a UserForm is a separate window whose appearance and behavior are handled in the VBA project.
When a UserForm Is Useful
A form can make a workbook easier to use when people should enter records in a consistent format or follow a sequence of choices. It is especially helpful when the underlying sheet contains formulas, lookup tables, or other information users should not need to navigate. A form can also perform checks before it writes values, reducing common entry mistakes.
Good examples include a simple expense log, a service-request register, a contact list, or a button-driven tool that asks for a date range before producing a report. A UserForm is not automatically the best interface for every spreadsheet. If users only need to enter a few values in a clear table, worksheet cells, data validation, or a formatted Excel table may be easier to build and maintain.
How UserForms Work
VBA UserForms are event-driven. A form can be loaded and displayed, and its code responds to events such as opening, clicking a button, changing a field, or closing. The form’s controls have names, properties, and events. For instance, a button named cmdSave can have a Click event procedure that checks the entered information and saves it.
The form’s visual design is separate from much of its behavior. You set properties such as a control’s name, caption, size, and position in the Properties window, then write code in the form’s code module. A standard module can contain a macro that opens the form. This separation makes it easier to identify which code belongs to the interface and which code performs broader workbook tasks.
By default, Show opens a modal form: the user must close or dismiss it before working elsewhere in Excel. A modeless form can stay open while the user interacts with the workbook, but that flexibility can create confusion if the worksheet changes while the form is open. Use modeless behavior only when the workflow genuinely needs it.
Create a Basic UserForm
In Excel for Microsoft 365 on Windows, first save a working copy of the workbook. Use the Developer tab’s Visual Basic command, or press Alt+F11, to open the Visual Basic Editor. If Developer is missing, enable it through File > Options > Customize Ribbon and select Developer.
- In the Visual Basic Editor, choose Insert > UserForm. The form designer and Toolbox appear. If the Toolbox is not visible, choose View > Toolbox.
- Click the form and use the Properties window to set its
(Name), such asfrmEntry, and itsCaption, such as “New record.” The name is used by VBA; the caption is what users see. - Drag controls from the Toolbox onto the form. Set each control’s
(Name)to a meaningful identifier, such astxtNamefor a text box orcmdSavefor a button. Give buttons readable captions. - Double-click a control to create or open its event procedure, then write the VBA for the action. Use the Project Explorer to select the form or a standard module when you need to edit different parts of the project.
- In a standard module, add a macro that displays the form. Run that macro from Excel or assign it to a worksheet button.
On Excel for Mac, VBA is available, but keyboard shortcuts can vary with the keyboard and macOS settings; use the Visual Basic command on the ribbon if needed. Mac support for controls and platform-specific VBA behavior differs from Windows, so test a form on every operating system where it will be used. Excel for the web does not create or run VBA macros, so a VBA UserForm is a desktop Excel feature.
A Small Save-Record Example
This example assumes a worksheet named Data has headers in row 1, with Name in column A and Department in column B. Create a form named frmEntry with a text box named txtName, a text box named txtDepartment, and a button named cmdSave. The code goes in the UserForm’s code module. Save a copy of the workbook before running a macro: VBA changes cannot be undone with Ctrl+Z.
Option Explicit
Private Sub cmdSave_Click()
Dim ws As Worksheet
Dim nextRow As Long
Dim personName As String
Dim department As String
' Read and trim the entered values.
personName = Trim$(CStr(Me.txtName.Value))
department = Trim$(CStr(Me.txtDepartment.Value))
' Require both fields before writing a record.
If Len(personName) = 0 Or Len(department) = 0 Then
MsgBox "Enter a name and a department.", vbExclamation
Exit Sub
End If
Set ws = ThisWorkbook.Worksheets("Data")
nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
' Add the record beneath the existing entries.
ws.Cells(nextRow, 1).Value = personName
ws.Cells(nextRow, 2).Value = department
MsgBox "Record saved.", vbInformation
Unload Me
End Sub
To open the form, put this macro in a standard module:
Option Explicit
Public Sub ShowEntryForm()
' Display the data-entry form.
frmEntry.Show
End Sub
The example writes the two entered values to columns A and B of the next row, then closes the form. It expects the sheet and controls to have exactly the names described; a missing worksheet or renamed control will cause an error. For a shared workbook, consider how simultaneous edits, duplicate records, and changes to the worksheet layout should be handled before relying on a simple next-row approach.
Validation and a Better User Experience
Validation should happen before data is written. The example checks for blank entries, but a real form may also need to check whether a date is valid, a number falls within an allowed range, or a selection comes from an approved list. A ComboBox populated with known departments can reduce spelling variations. Give users clear instructions and identify the field that needs attention rather than showing a vague error message.
Plan how the form opens, saves, resets, and closes. A Cancel button should leave the worksheet unchanged; a Reset button should clear the fields without saving. Avoid silently discarding information if a user closes the window after entering data. For longer forms, group related fields and make tab order sensible so users can move through controls with the keyboard. Test the form with realistic entries, empty values, long text, and unexpected input.
Saving, Sharing, and Macro Safety
A workbook containing VBA must be saved in a macro-enabled format such as .xlsm if the VBA project is to be retained. Saving as .xlsx removes the macros. When another person opens the file, Excel’s macro security settings may block its code. Users should enable macros only for workbooks they trust and understand; a form is not a way to bypass Excel’s security protections.
For managed distribution, an organization may use digitally signed macros and approved trusted locations, according to its security policy. Do not ask users to lower global macro security just to make one workbook work. Also remember that a macro can change or delete workbook data, and Ctrl+Z does not reverse VBA actions. Keep a backup and make destructive actions explicit, with confirmation where appropriate.
Common Problems and Practical Limits
- The form does not open: check that macros are permitted, the file is saved as
.xlsm, and the form name in the macro matches its actual(Name). - A control reference fails: confirm the control’s name in the Properties window. Its visible caption is not necessarily the name used in code.
- Data goes to an unexpected row: verify the assumed worksheet, headers, and column used to find the last occupied row. A blank key column can make a simple last-row method unreliable.
- The form looks different on another computer: window scaling, fonts, Excel versions, and operating systems can affect layout. Test at the display settings and platforms your audience uses.
- It will not work in a browser: Excel for the web does not run VBA UserForms. A desktop workbook is required for this interface.
UserForms also require someone to maintain VBA code as the workbook and its data structure change. They are not a substitute for access control or a multi-user database. If several people need simultaneous data entry, a shared service or database-backed application may be more appropriate than writing records into one workbook.
Alternatives to a VBA UserForm
For straightforward entry, a formatted Excel table with clear headings may be enough. Data validation can constrain entries or provide drop-down choices; on the Windows ribbon, find it at Data > Data Validation. Worksheet-based entry is usually easier to inspect and works without a custom VBA window, though it does not provide the same guided interface.
Other options depend on the job and the organization’s Microsoft 365 setup. Microsoft Forms can collect responses through a web form, while Power Apps can build more elaborate business interfaces. These tools have their own licensing, sharing, and data-storage considerations. Choose a UserForm when the task is a desktop Excel workflow and the benefits of a tailored interface justify maintaining VBA.
Frequently Asked Questions
How do I open a UserForm in Excel? Create a macro in a standard VBA module that calls the form’s name followed by .Show, then run the macro. A worksheet button can also be assigned to that macro.
Can I use a VBA UserForm in Excel for the web? No. Excel for the web does not run VBA macros or VBA UserForms. Open the macro-enabled workbook in desktop Excel to use the form.
What file type should I use for a UserForm? Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) to retain its VBA project. An .xlsx file does not preserve VBA macros.
Can a UserForm write information to a worksheet? Yes. Its event code can read control values and assign them to worksheet cells, as in the example above. The code must identify the intended sheet and handle invalid or missing input.
Can I undo a UserForm macro with Ctrl+Z? Generally, no. Excel’s undo history does not reverse changes made by VBA macros. Save a backup before running code that changes workbook data.
Related
- Split Cell Data Into Columns in Excel: Best Methods
- Power Query Merge: How to Join Tables in Microsoft Excel
- Conditional Formatting Greater Than: Excel Rules Explained
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA String Comparison: Rules, Methods, and ExamplesIn Excel VBA, compare strings with StrComp when you want to choose case sensitivity explicitly: use StrComp(firstText, secondText,…
- VBA Pivot Table Delete: What to Remove and HowYes. In Excel for Microsoft 365 on Windows, remove a PivotTable report with VBA by clearing its full…
- VBA Copy and Paste a Range of Cells in ExcelTo copy a range of cells with VBA, use sourceRange.Copy Destination:=destinationRange. For example, Worksheets("Sheet1").Range("A1:C10").Copy Destination:=Worksheets("Sheet2").Range("A1") copies the range…
- Excel VBA Active Worksheet: How It Works and When to Use ItIn Excel VBA, ActiveSheet refers to the sheet currently selected in the active workbook. It is not a…
- VBA: How to Find an Accredited VA PractitionerIf by VBA you mean the U.S. Department of Veterans Affairs’ Veterans Benefits Administration, use VA’s official accreditation…
- Excel VBA UserForm Activate vs InitializeIn Excel VBA, use UserForm_Initialize to set up a form when it is loaded, and use UserForm_Activate for…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.