Data entry is one of the most common tasks performed in Excel. VBA can automate data entry by collecting information, finding the correct row, writing values into cells, and repeating the process whenever required.
Instead of manually entering the same type of information again and again, a VBA program can automatically enter student IDs, names, courses, marks, fees, dates, and other information into an Excel worksheet.
Data entry means entering information into an Excel worksheet.
For example, a student record may contain:
Normally, these values can be entered manually into cells.
Manual data entry can become repetitive when many records have to be entered. VBA can automate this process.
Automation can help to:
Suppose a student sheet contains hundreds of records. For every student, you may need to enter the same fields:
Student ID
Student Name
Course
Marks
Entering these fields manually for every student can take time. A VBA procedure can automate the process.
Before creating a data-entry macro, prepare a worksheet with suitable headings.
| Column | Heading |
|---|---|
| A | Student ID |
| B | Student Name |
| C | Course |
| D | Marks |
| E | Admission Date |
Assume the worksheet is named Students.
To create a VBA data-entry program, open the VBA Editor.
You can use the keyboard shortcut:
Alt + F11
Then insert a standard module:
Insert → Module
The VBA code can be written inside the module.
Start by creating a Sub procedure.
Sub AddStudent()
End Sub
The code required for the data-entry system will be placed between Sub AddStudent() and End Sub.
Variables can store the information entered by the user.
Dim studentID As String
Dim studentName As String
Dim course As String
Dim marks As Double
Each variable represents a different piece of student information.
The InputBox function can ask the user to enter a student ID.
studentID = InputBox("Enter Student ID")
The entered value is stored in the studentID variable.
The student's name can be collected using another InputBox.
studentName = InputBox("Enter Student Name")
The entered name is stored in studentName.
The course can also be collected from the user.
course = InputBox("Enter Course")
For example, the user might enter ADCA, Tally, or Web Development.
Marks can be collected from the user.
marks = Val(InputBox("Enter Marks"))
The Val function converts numeric text entered by the user into a number that can be stored in a numeric variable.
The VBA Date function returns the current system date.
Dim admissionDate As Date
admissionDate = Date
This date can then be written into the worksheet automatically.
A data-entry program should normally add the new record below the existing records instead of overwriting them.
Dim nextRow As Long
nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
This finds the next available row based on column A.
For a reliable program, use a worksheet variable.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Students")
Now the variable ws represents the Students worksheet.
Once the worksheet variable is available, find the next row using that worksheet.
nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
This searches column A of the Students worksheet for the next available row.
The student ID can now be written into column A.
ws.Cells(nextRow, 1).Value = studentID
The value is written into the next available row in column A.
The student's name can be written into column B.
ws.Cells(nextRow, 2).Value = studentName
The name is stored in the same row as the student ID.
The course is stored in column C.
ws.Cells(nextRow, 3).Value = course
This keeps the complete student record in the same row.
Marks can be written into column D.
ws.Cells(nextRow, 4).Value = marks
The value stored in the marks variable is placed in column D.
The automatically generated admission date can be written into column E.
ws.Cells(nextRow, 5).Value = admissionDate
This saves the current date with the student's record.
The following procedure combines the main steps.
Sub AddStudent()
Dim ws As Worksheet
Dim nextRow As Long
Dim studentID As String
Dim studentName As String
Dim course As String
Dim marks As Double
Dim admissionDate As Date
Set ws = ThisWorkbook.Worksheets("Students")
studentID = InputBox("Enter Student ID")
studentName = InputBox("Enter Student Name")
course = InputBox("Enter Course")
marks = Val(InputBox("Enter Marks"))
admissionDate = Date
nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
ws.Cells(nextRow, 1).Value = studentID
ws.Cells(nextRow, 2).Value = studentName
ws.Cells(nextRow, 3).Value = course
ws.Cells(nextRow, 4).Value = marks
ws.Cells(nextRow, 5).Value = admissionDate
MsgBox "Student Added Successfully"
End Sub
You can format the date after writing it.
ws.Cells(nextRow, 5).NumberFormat = "dd-mm-yyyy"
This displays the date in day-month-year format.
You can check whether required information was entered before saving the record.
If studentID = "" Then
MsgBox "Student ID is required"
Exit Sub
End If
The procedure stops if the student ID is empty.
You can also validate marks before writing them.
If marks < 0 Or marks > 100 Then
MsgBox "Enter marks between 0 and 100"
Exit Sub
End If
This prevents invalid marks from being entered.
The following example combines input, validation, next-row detection, and data entry.
Sub AddStudent()
Dim ws As Worksheet
Dim nextRow As Long
Dim studentID As String
Dim studentName As String
Dim course As String
Dim marks As Double
Dim admissionDate As Date
Set ws = ThisWorkbook.Worksheets("Students")
studentID = InputBox("Enter Student ID")
If studentID = "" Then
MsgBox "Student ID is required"
Exit Sub
End If
studentName = InputBox("Enter Student Name")
If studentName = "" Then
MsgBox "Student Name is required"
Exit Sub
End If
course = InputBox("Enter Course")
If course = "" Then
MsgBox "Course is required"
Exit Sub
End If
marks = Val(InputBox("Enter Marks"))
If marks < 0 Or marks > 100 Then
MsgBox "Enter marks between 0 and 100"
Exit Sub
End If
admissionDate = Date
nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
ws.Cells(nextRow, 1).Value = studentID
ws.Cells(nextRow, 2).Value = studentName
ws.Cells(nextRow, 3).Value = course
ws.Cells(nextRow, 4).Value = marks
ws.Cells(nextRow, 5).Value = admissionDate
ws.Cells(nextRow, 5).NumberFormat = "dd-mm-yyyy"
MsgBox "Student Added Successfully"
End Sub
A data-entry procedure can be run whenever a new student needs to be added.
Sub AddNewStudent()
AddStudent
End Sub
The main data-entry procedure can be kept in a standard module and run from a button, shape, or keyboard shortcut.
A typical automated data-entry workflow is:
Input
↓
Validation
↓
Find Next Row
↓
Write Data
↓
Format Data
↓
Confirmation
Automating data entry is one of the most practical uses of Excel VBA. Instead of manually entering every record, VBA can collect information and store it automatically in the correct worksheet and row.
The basic process is:
studentID = InputBox("Enter Student ID")
studentName = InputBox("Enter Student Name")
nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
ws.Cells(nextRow, 1).Value = studentID
ws.Cells(nextRow, 2).Value = studentName
You can extend this concept by adding courses, marks, fees, dates, attendance, contact information, and other fields.
You can also add validation:
If studentID = "" Then
MsgBox "Student ID is required"
Exit Sub
End If
This makes the program more reliable and suitable for practical Excel projects such as student management systems, fee records, attendance systems, and data-entry applications.
Question: Which VBA expression is commonly used to find the next available row based on column A?