Data Entry Using UserForm - Excel VBA Form Controls
Data Entry Using UserForm - Excel VBA Form Controls
In today’s example we’ll introduce data entry with the help of the UserForm. Using UserForm can
considerably improve the interaction with our users. Furthermore this way we can assist them to
execute particular steps.
They play a very important role in the automatization of Excel. In these days none of the business
dashboards can be imagined without Form Controls.
Let’s say a few words about today’s task. At times data entry can be very boring and by this can
incline users to mistakes. We’ll design a form that based on three data (Name, Age, Job title) will
upload a “database”.
This
Whatwebsite uses cookies
can exactly to improve
be seen on the your experience.
picture? We'll assume
Three label, you'reelds.
three text ok with this, but you can opt-out if you
wish. Read More
And another thing, the Command Button. We’ll assign to this a simple macro that will store input
data.
Open the VBA Editor. If the Project Explorer is not visible, click View, Project Explorer. Click Insert,
and select the Userform.
Accept
After this place the elds and their names onto this form.
Now insert the names of the elds by clicking on the Label Control button:
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you
wish. Read More
As next step follow the TexBox belonging to the names. This option enables the data insertion.
Change any elements and attributes of our form by right-clicking on them and then choose the
Properties window.
As next step we have the option for example to set the fonts, size and colors.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you
wish. Read More
We can be all creative regarding the designs. We can make any kind we’d like or what we need for a
given task.
Now we will place another button on this form. The purpose of this is to x the given data on one
Excel table.
Let’swebsite
This see what
usesthe datatoentry
cookies form
improve looks
your like now:
experience. We'll assume you're ok with this, but you can opt-out if you
wish. Read More
The user interface is ready! Data input will be a lot easier from now on. But we still have one very
important thing to do.
Because for the next step we have to determine the code behind it.
In this example this would run by clicking on the “Add to list” button.
From the drop down menu choose the View Code command. In the next chapter we’ll show how to
assign a short VBA code to this.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you
wish. Read More
Adding Logic to Button in a Form
Let’s clear what do we expect from the code after clicking on the button?
Our expectation will be that it inserts the values of the UserForm into the determined cells of the
determined rows.
For this we have to know where the next empty line in the table is.
After the insertion we have to equal the appropriate cells to the input values of the appropriate
elds.
The rst one with Integer type by the name rw to determine the current, still empty rows.
This will determine which worksheet our form will refresh / update the cells.
We can already set the calues of the latter variable onto Sheet1.
Use the Range Find method to determine the starting value of our rw variable.
In our case we have to use the ws.Cells.Find method, because we are looking amongst the cells of
Sheet1 that contains anything (What:=”*”).
The written value in the eld must be equal with the rw variable. We store the number of the cells
in the rw variable.
We can nd out the names of our elds from the UserForm Properties window in case we did not
use standard names for them.
Therefore, ws.Cells(rw, 1).Value (this is the cell of Sheet1 rst empty row in the rst column) be
equal to Me.TextBox1.Value which is the value was given into our rst eld.
In this case it refers to UserForm, but if we was to write our code on a Sheet, that it would refer to
that.
At this time on the popup massage window there should be only one OK button (vbOKOnly
parameter) but of course with the help of the other parameters of the MsgBox we can tune this to
our liking.
If it is done we can delete the input values from our elds. We can do this because they are already
in the appropriate cells.
After this is useful to bring back the cursor to the rst eld with the help of the SetFocus parameter.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you
wish. Read More
Error handling – How can we do this?
Basically we have to endeavor to write the codes with minimal possible errors.
For this it is useful to insert an “If” branching so in case of empty cells the UserForm will do nothing.
Our goal is to only store the data if all the cells / elds are lled out! The following short code will
alert the user for insu cient data.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you
wish. Read More
What else is there left to do? To determine how to start the UserForm.
In our example we choose the simplest solution. We assign the startup of the code to a
CommandButton.
Under the Ribbon Developer tab the Controls section can be found.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you
wish. Read More
By double clicking on the button in Desingn Mode we get over to the Visusal Basic Editor.
We only need to show the UserForm named by us DataEntryForm command for the “Click” event.
After clicking on the button the UserForm opens. After lling this out the data gets into the
appropriate cells.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you
Conclusion: wish. Read More