In Microsoft Excel, you can create a custom UserForm that provides a simple
interface for entering data. This article includes steps for creating a
custom UserForm and a sample Visual Basic for Applications macro that
places the data that you enter in a worksheet.
Creating a Custom UserForm
To create a custom UserForm, follow these steps:
- Save and close any open workbooks, and then create a new workbook.
- Type the following in Sheet1:
A1: Name B1: ID C1: Phone
- Start the Visual Basic Editor (press OPTION+F11).
- On the Insert menu, click UserForm.
- Add three text box controls (TextBox1, TextBox2, and TextBox3) to the
UserForm, and space them out vertically so that you can add a label
control above each text box control.
- Add three label controls (Label1, Label2, and Label3), one above each
of the text box controls that you added.
- Press F6 to display the Properties window.
- Change the Caption properties for each of the Label controls to the following:
Control Caption property
- Add two command button controls (CommandButton1 and CommandButton2) to the UserForm.
- Change the Caption properties for each of the CommandButton controls to the following:
Control Caption property
CommandButton1 Add Record
Sample Macro for Adding UserForm Data to a Worksheet
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
To attach macro code to the controls on the UserForm, follow these
- Double-click the UserForm to display the code module that is associated with the UserForm.
- Type the following code for the CommandButton1 Click event:
Private Sub CommandButton1_Click()
Dim LastRow As Range
Dim response As Integer
Set LastRow = Sheet1.Range("a65536").End(xlUp)
LastRow.Offset(1, 0).Value = TextBox1.Text
LastRow.Offset(1, 1).Value = TextBox2.Text
LastRow.Offset(1, 2).Value = TextBox3.Text
MsgBox "One record written to Sheet1"
response = MsgBox("Do you want to enter another record?", _
If response = vbYes Then
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
- Type the following code for the CommandButton2 Click event:
Private Sub CommandButton2_Click()
- On the Insert menu, click Module.
- In this module, type the following code:
Using the UserForm for Data Entry
To use the UserForm, follow these steps:
- In the Visual Basic Editor, click Close and Return to Microsoft Excel on the File menu.
- Save the workbook.
- Run the Show_UserForm macro (press OPTION+F8, and then double-click
Show_UserForm in the list of macros).
The UserForm is displayed, and you can start typing data in the three
- After you type the data, click Add Record.
The data that you typed is placed in Sheet1 under the field headers in row 1.
- When you are prompted about whether to add another record, click Yes to continue or No to close the UserForm.
For more information about custom UserForms, in the Visual Basic Editor, click the Office Assistant
, type creating a custom dialog box
, click Search
, and then click a topic to view it.NOTE
: If the Assistant is hidden, click the Office Assistant
button on the Standard
Article ID: 184254 - Last Review: October 7, 2013 - Revision: 2.1
- Microsoft Excel 2001 for Mac
- Microsoft Excel 98 for Macintosh
|kbnosurvey kbarchive kbhowto kbprogramming KB184254|