A data entry form makes it easier to enter student information into an Excel worksheet. Instead of typing directly into cells, we can use VBA controls such as TextBox, Label, and CommandButton.
A data entry form is a user interface that allows users to enter information without directly typing into worksheet cells.
For example, a student form can collect:
Data entry forms make Excel applications easier to use.
To create a UserForm, open the VBA Editor.
Use:
Developer → Visual Basic → Insert → UserForm
Excel creates a blank UserForm where controls can be added.
A UserForm is a window that contains controls used to interact with the user.
Common controls include:
A Label displays text that tells the user what information should be entered.
For example:
Student ID
Student Name
Course
Mobile Number
Labels help make the form easy to understand.
A TextBox allows the user to enter information.
For example, create a TextBox for Student ID and set its name to:
txtStudentID
The TextBox value can later be read using its Value property.
Add another TextBox for the student's name.
Set its Name property to:
txtStudentName
The user can enter the student's full name in this TextBox.
Add a TextBox for the student's course.
txtCourse
The course entered by the user will later be stored in the worksheet.
Add a TextBox for the student's mobile number.
txtMobile
The value entered into this TextBox can be saved in the worksheet.
Add a CommandButton to save the entered information.
Set its Name property to:
cmdSave
Set its Caption property to:
Save
A Clear button can remove the values entered in the form.
Set its Name to:
cmdClear
Set its Caption to:
Clear
The value entered into a TextBox can be accessed using the Value property.
txtStudentID.Value
Similarly:
txtStudentName.Value
txtCourse.Value
txtMobile.Value
Double-click the Save button to create its Click event.
Private Sub cmdSave_Click()
End Sub
Code placed inside this procedure runs when the Save button is clicked.
Suppose our student data is stored in a worksheet named Students.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Students")
The variable ws now represents the Students worksheet.
New student records should be inserted into the next available row.
Dim nextRow As Long
nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
This finds the last used row in column A and adds 1.
Suppose column A contains Student ID.
ws.Cells(nextRow, 1).Value = txtStudentID.Value
The value from the Student ID TextBox is saved into column A.
Student Name can be saved in column B.
ws.Cells(nextRow, 2).Value = txtStudentName.Value
Each new record is written to the calculated next row.
Course information can be saved in column C.
ws.Cells(nextRow, 3).Value = txtCourse.Value
The course entered in the form is stored automatically.
Mobile Number can be saved in column D.
ws.Cells(nextRow, 4).Value = txtMobile.Value
The complete record is now being saved into the worksheet.
After saving the record, we can inform the user that the operation was successful.
MsgBox "Student record saved successfully."
This gives immediate feedback to the user.
The TextBox values can be cleared after saving.
txtStudentID.Value = ""
txtStudentName.Value = ""
txtCourse.Value = ""
txtMobile.Value = ""
This prepares the form for entering another student.
Double-click the Clear button and add the following code:
Private Sub cmdClear_Click()
txtStudentID.Value = ""
txtStudentName.Value = ""
txtCourse.Value = ""
txtMobile.Value = ""
End Sub
Clicking Clear will remove all entered values.
Before saving, we should check whether important fields are empty.
If txtStudentID.Value = "" Then
MsgBox "Please enter Student ID."
Exit Sub
End If
This prevents an incomplete record from being saved.
We can also check whether the Student Name has been entered.
If txtStudentName.Value = "" Then
MsgBox "Please enter Student Name."
Exit Sub
End If
Similar checks can be added for other required fields.
The Save button can now combine validation and data saving.
Private Sub cmdSave_Click()
Dim ws As Worksheet
Dim nextRow As Long
If txtStudentID.Value = "" Then
MsgBox "Please enter Student ID."
Exit Sub
End If
If txtStudentName.Value = "" Then
MsgBox "Please enter Student Name."
Exit Sub
End If
Set ws = ThisWorkbook.Worksheets("Students")
nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
ws.Cells(nextRow, 1).Value = txtStudentID.Value
ws.Cells(nextRow, 2).Value = txtStudentName.Value
ws.Cells(nextRow, 3).Value = txtCourse.Value
ws.Cells(nextRow, 4).Value = txtMobile.Value
MsgBox "Student record saved successfully."
txtStudentID.Value = ""
txtStudentName.Value = ""
txtCourse.Value = ""
txtMobile.Value = ""
End Sub
A separate macro can be used to open the data entry form.
Sub OpenStudentForm()
UserForm1.Show
End Sub
Replace UserForm1 with the actual name of your UserForm.
We can also save the current date when a student record is added.
Suppose column E contains the Date of Entry.
ws.Cells(nextRow, 5).Value = Date
VBA automatically inserts the current date.
A professional student data entry form can include:
Common mistakes while creating a UserForm include:
The following code provides a complete basic Save button for a student data entry form.
Private Sub cmdSave_Click()
Dim ws As Worksheet
Dim nextRow As Long
If txtStudentID.Value = "" Then
MsgBox "Please enter Student ID."
Exit Sub
End If
If txtStudentName.Value = "" Then
MsgBox "Please enter Student Name."
Exit Sub
End If
If txtCourse.Value = "" Then
MsgBox "Please enter Course."
Exit Sub
End If
Set ws = ThisWorkbook.Worksheets("Students")
nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
ws.Cells(nextRow, 1).Value = txtStudentID.Value
ws.Cells(nextRow, 2).Value = txtStudentName.Value
ws.Cells(nextRow, 3).Value = txtCourse.Value
ws.Cells(nextRow, 4).Value = txtMobile.Value
ws.Cells(nextRow, 5).Value = Date
MsgBox "Student record saved successfully."
txtStudentID.Value = ""
txtStudentName.Value = ""
txtCourse.Value = ""
txtMobile.Value = ""
End Sub
This form provides a simple foundation for creating a complete Excel VBA student management application.
Question: Which VBA control is commonly used to allow a user to enter text in a UserForm?