How To Add A UserForm To Aid Data Entry in Excel - TechRepublic
How To Add A UserForm To Aid Data Entry in Excel - TechRepublic
Not everyone is familiar with Excel. You can protect the data input process by using Excel's
UserForm object to collect data. Susan Harkins explains how.
When you need a user-friendly data input process, create an Excel (https://microsoft-
us.evyy.net/c/148636/434427/3327?subId1=techrepublic) UserForm. Users who are unfamiliar with Excel
will find these forms much easier to navigate than a sheet. Besides ease of use, they're great
tools for restricting choices. In this article, I'll show you how to create a UserForm object, add
controls to it, and then use Visual Basic for Applications (VBA) to link the controls to a sheet.
You can work with your own data or download the example .xls
(http://b2b.cbsimg.net/downloads/Thompson/ExcelUserForm.xls) or .xlsm
(http://b2b.cbsimg.net/downloads/Thompson/ExcelUserForm.xlsm) file.
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 1/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
Figure A
We'll create a simple UserForm that will help users enter new records into this data set.
Rich Female Celebrities Who You Never Knew Have Even Richer Hubbies
With a bit of data in a worksheet, you're ready to move to the Visual Basic Editor (VBE) to
create the UserForm:
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 2/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
3. Press [F4] to display the UserForm's property sheet and enter a name in the Name control.
When you do, the VBE updates the property dialog's title, accordingly (Figure C).
Figure C
Add controls
The blank UserForm is ready for input controls. We'll add a few text box, combo box, and
command button controls. To add the first text box, do the following:
1. With the UserForm selected, click Text Box in the Toolbox and then drop it onto the form. If
the Toolbox isn't visible, be sure to click the UserForm. It's visible when the UserForm is
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 3/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
selected.
2. With the new text box control selected, name it txtGivenName using the property sheet,
which will update for the selected element (Figure D).
Figure D
Using Figure E as a guide, add the additional controls listed in Table A and name them. The
labels aren't necessary on this simple example, but when creating a UserForm for your own
data, you'll probably want to include them. When you do, Excel's default names are usually
adequate. Enter a descriptive caption for each label and the two command buttons. It's a good
idea to save your work as you progress.
Figure E
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 4/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
Table A
Add code
The code behind the form makes it work. Don't let the amount of code intimidate you. The
Save command button does most of the work. Once you enter all the values, the code in this
button will transfer the entered values to the sheet. To add code, double-click the UserForm to
open its module and enter the procedures in Listing A. (Don't try to copy and paste from this
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 5/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
web page because the VBE will object to some web characters. Instead, download one of the
example files: Module1.bas (http://b2b.cbsimg.net/downloads/Thompson/Module1.bas) or ufrmAnimals.frm
(http://b2b.cbsimg.net/downloads/Thompson/ufrmAnimals.frm).)
Listing A
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 6/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
Me.cboClass.Value = ""
Me.txtGivenName.Value = ""
Me.txtTagNumber.Value = ""
Me.txtSpecies.Value = ""
Me.cboSex.Value = ""
Me.cboConservationStatus.Value = ""
Me.txtComment.Value = ""
End Sub
Private Sub cmdClose_Click()
'Close UserForm.
Unload Me
End Sub
The first three procedures populate the three combo box controls. The fourth procedure,
cmdAdd_Click(), copies the input values from the UserForm to the data range in the sheet and
then clears the controls so you can enter another record. This way, the user can enter multiple
records quickly. The last procedure, cmdClose_Click(), closes the UserForm.
The procedure in Listing B opens the UserForm. In the next section, we'll add a macro button
that calls this procedure to the Quick Access Toolbar (QAT). Don't add this procedure to the
UserForm's module. Instead, choose Module from the Insert menu and add the short
procedure.
Listing B
Sub ShowAnimalsUF()
'Display Animals UserForm.
ufrmAnimals.Show Modal
End Sub
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 7/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
4. Click Close.
Figure G
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 8/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
To copy those values to the worksheet, click the Save Animal button. The code behind the
UserForm will copy the values from the UserForm controls to the sheet and clear the controls
for the next record, as shown in Figure H. To close the form, click the Close button.
Figure H
Worth noting
None of the code saves the record once it's copied to the sheet. You must remember to save
the workbook to save new records at the sheet level. You can add appropriate code to the
UserForm or allow users to do that themselves. In addition, there's no error-handling or data
validation in this bare-bones code. Using a combo box or list box helps you limit choices, but
you'll need additional code to protect the validity of text and number values and to force users
to enter values for required fields. As you can see, the example form allows the user to leave
fields empty.
Be your company's Microsoft insider with the help of these Windows and Office tutorials and our
experts' analyses of Microsoft's enterprise products. Delivered Mondays and Wednesdays
Sign up today ()
Also see
How to add a drop-down list to an Excel cell (https://www.techrepublic.com/blog/microsoft-office/how-
to-add-a-drop-down-list-to-an-excel-cell/) (TechRepublic)
How to build a successful data scientist career (free PDF) (https://www.techrepublic.com/resource-
library/whitepapers/how-to-build-a-successful-data-scientist-career-free-pdf/) (TechRepublic)
Six clicks: Excel power tips to make you an instant expert (https://www.zdnet.com/pictures/six-
clicks-excel-power-tips-to-make-you-an-instant-expert/) (ZDNet)
Office Q&A: How to update UserForm VBA code to accommodate an Excel Table
(https://www.techrepublic.com/article/office-q-a-how-to-update-userform-vba-code-to-accommodate-an-excel-
table/)(TechRepublic)
You've been using Excel wrong all along (and that's OK) (https://www.zdnet.com/article/youve-been-
using-excel-wrong-all-along-and-thats-ok/) (ZDNet)
10 steps to creating a Word userform for addressing letters (https://www.techrepublic.com/blog/10-
things/10-steps-to-creating-a-word-userform-for-addressing-letters/) (TechRepublic)
Affiliate disclosure: TechRepublic may earn a commission from the products and services
featured on this page.
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 10/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
DOWNLOAD NOW
DOWNLOAD NOW
DOWNLOAD NOW
DOWNLOAD NOW
DOWNLOAD NOW
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 11/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
EDITOR'S PICKS
Inside the Raspberry Pi: The story of the $35 How self-driving tractors, AI, and precision
computer that changed the world agriculture will save us from the impending food
crisis
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 12/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
Smart farming: How IoT, robotics, and AI are Agriculture 4.0: How digital farming is
tackling one of the biggest problems of the century revolutionizing the future of food
The Brexit dilemma: Will London's start-ups stay or Can Russian hackers be stopped? Here's why it
go? might take 20 years
By Susan Harkins
Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she
was editor in chief for The Cobb Group, the world's largest publisher of technical journals.
MICROSOFT ON ZDNET
He Lied To His Mum He Was Working Just To Do His Business. Find Out What He Did
Ace Profits Academy
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 13/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
How SMBs can better protect their business and customer data
SHOW COMMENTS
EDITOR'S PICKS
Inside the Raspberry Pi: The story of the $35 How self-driving tractors, AI, and precision
computer that changed the world agriculture will save us from the impending food
crisis
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 14/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
Smart farming: How IoT, robotics, and AI are Agriculture 4.0: How digital farming is
tackling one of the biggest problems of the century revolutionizing the future of food
WATCH NOW
WATCH NOW
IBM Global Mailbox Solutions Brief: Enabling always-on operations and great...
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 15/16
4/20/2019 How to add a UserForm to aid data entry in Excel - TechRepublic
DOWNLOAD NOW
https://www.techrepublic.com/article/pro-tip-add-a-userform-to-aid-data-entry-in-excel/ 16/16