In this lesson, you will learn how to create a simple student record search system using Excel VBA. The user can enter a Student ID and VBA can search the worksheet to find the matching student record.
Student record searching means finding a particular student's information from a list of students.
For example, a worksheet may contain:
Instead of manually checking every row, VBA can search the records automatically.
Suppose the worksheet contains the following data:
| Student ID | Name | Course | Marks |
|---|---|---|---|
| 101 | Rahul | ADCA | 85 |
| 102 | Amit | Web Development | 78 |
| 103 | Priya | Tally | 92 |
VBA can search the Student ID column and display the matching record.
Create a worksheet named Students.
Use these headings:
A1 = Student ID
B1 = Name
C1 = Course
D1 = Marks
Enter student records below these headings.
An InputBox can be used to ask the user for the Student ID.
studentID = InputBox("Enter Student ID:")
The value entered by the user is stored in the variable studentID.
We need a variable to store the Student ID entered by the user.
Dim studentID As String
Using String is useful because IDs may sometimes contain letters as well as numbers.
The InputBox can be assigned directly to the variable.
studentID = InputBox("Enter Student ID:")
For example, if the user enters 102, the variable contains the value 102.
The user may press Cancel or leave the InputBox empty. We can check this before searching.
If studentID = "" Then
MsgBox "Please enter Student ID."
Exit Sub
End If
This prevents unnecessary searching when no ID is provided.
We can create a worksheet variable to work with the Students sheet.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Students")
Now ws represents the Students worksheet.
We need to know how many student records are present.
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
This finds the last used row in column A.
A For loop can check each student record one by one.
Dim i As Long
For i = 2 To lastRow
Next i
The loop starts from row 2 because row 1 contains the headings.
Inside the loop, compare the entered ID with the Student ID in column A.
If CStr(ws.Cells(i, 1).Value) = studentID Then
End If
Cells(i, 1) represents column A of the current row.
We can use a Boolean variable to remember whether the student was found.
Dim found As Boolean
found = False
Initially, the value is False because the record has not been found.
When the matching Student ID is found, change the value to True.
found = True
This tells VBA that the requested student exists in the worksheet.
Column B contains the student's name.
studentName = ws.Cells(i, 2).Value
Here, 2 represents column B.
Column C contains the course.
courseName = ws.Cells(i, 3).Value
The third argument represents column C.
Column D contains the student's marks.
marks = ws.Cells(i, 4).Value
Column D is represented by number 4.
We can declare variables for the student's information.
Dim studentName As String
Dim courseName As String
Dim marks As Variant
These variables will temporarily store the matching student's data.
After finding the student, we can display the information using MsgBox.
MsgBox "Student ID: " & studentID & vbCrLf & _
"Name: " & studentName & vbCrLf & _
"Course: " & courseName & vbCrLf & _
"Marks: " & marks
vbCrLf creates a new line in the message.
Once the student is found, there is no need to continue searching.
Exit For
This immediately stops the For loop.
After the loop, check whether the student was found.
If found = False Then
MsgBox "Student record not found."
End If
This message is displayed when no matching Student ID exists.
The basic search macro can now be combined into one procedure.
Sub SearchStudent()
Dim studentID As String
Dim studentName As String
Dim courseName As String
Dim marks As Variant
Dim lastRow As Long
Dim i As Long
Dim found As Boolean
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Students")
studentID = InputBox("Enter Student ID:")
If studentID = "" Then
MsgBox "Please enter Student ID."
Exit Sub
End If
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
found = False
For i = 2 To lastRow
If CStr(ws.Cells(i, 1).Value) = studentID Then
studentName = ws.Cells(i, 2).Value
courseName = ws.Cells(i, 3).Value
marks = ws.Cells(i, 4).Value
found = True
MsgBox "Student ID: " & studentID & vbCrLf & _
"Name: " & studentName & vbCrLf & _
"Course: " & courseName & vbCrLf & _
"Marks: " & marks
Exit For
End If
Next i
If found = False Then
MsgBox "Student record not found."
End If
End Sub
The macro follows these steps:
The same technique can be used to search by name.
Since the student name is stored in column B, compare column 2.
If LCase(ws.Cells(i, 2).Value) = LCase(studentName) Then
LCase makes the comparison case-insensitive.
The Like operator can be used when you want to search for part of a student's name.
If LCase(ws.Cells(i, 2).Value) Like "*" & LCase(studentName) & "*" Then
End If
The asterisk allows additional characters before or after the searched text.
We can highlight the row when a student is found.
ws.Rows(i).Interior.ColorIndex = 6
This applies a yellow background to the matching row.
This can make the searched record easier to identify.
Instead of displaying the result only in a MsgBox, the result can also be written to another worksheet.
Worksheets("Search").Range("B2").Value = studentID
Worksheets("Search").Range("B3").Value = studentName
Worksheets("Search").Range("B4").Value = courseName
Worksheets("Search").Range("B5").Value = marks
This is useful when creating a professional student search system.
A student search system can be improved by adding:
Suppose the user enters Student ID 103.
VBA searches column A and finds the matching row. It then reads the student's name, course, and marks.
The result could be displayed as:
Student ID: 103
Name: Priya
Course: Tally
Marks: 92
While creating a search system, avoid these common mistakes:
The following macro provides a complete basic Student ID search system.
Sub SearchStudent()
Dim ws As Worksheet
Dim studentID As String
Dim studentName As String
Dim courseName As String
Dim marks As Variant
Dim lastRow As Long
Dim i As Long
Dim found As Boolean
Set ws = ThisWorkbook.Worksheets("Students")
studentID = InputBox("Enter Student ID:")
If studentID = "" Then
MsgBox "Please enter Student ID."
Exit Sub
End If
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
found = False
For i = 2 To lastRow
If CStr(ws.Cells(i, 1).Value) = studentID Then
studentName = ws.Cells(i, 2).Value
courseName = ws.Cells(i, 3).Value
marks = ws.Cells(i, 4).Value
found = True
ws.Rows(i).Interior.ColorIndex = 6
MsgBox "Student ID: " & studentID & vbCrLf & _
"Name: " & studentName & vbCrLf & _
"Course: " & courseName & vbCrLf & _
"Marks: " & marks
Exit For
End If
Next i
If found = False Then
MsgBox "Student record not found."
End If
End Sub
This type of search system is a good foundation for building a complete Excel VBA student management project.
Question: Which VBA statement is useful for stopping a For loop after a student record has been found?