Lesson 56 of 60 – Searching Student Records
93%

Searching Student Records in Excel VBA

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.

Note: Searching records is a very useful feature when working with large student databases in Excel.

1. What is Student Record Searching?

Student record searching means finding a particular student's information from a list of students.

For example, a worksheet may contain:

  • Student ID
  • Student Name
  • Course
  • Marks

Instead of manually checking every row, VBA can search the records automatically.

2. Example Student Database

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.

3. Preparing the Worksheet

Create a worksheet named Students.

Use these headings:

A1 = Student ID
B1 = Name
C1 = Course
D1 = Marks

Enter student records below these headings.

4. Using InputBox for Searching

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.

5. Declaring the Student ID Variable

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.

6. Getting the Student ID

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.

7. Checking for Empty Input

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.

8. Declaring the Worksheet Variable

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.

9. Declaring the Last Row

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.

10. Using a For Loop

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.

11. Comparing Student IDs

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.

12. Declaring a Found Variable

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.

13. Setting Found to True

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.

14. Reading the Student Name

Column B contains the student's name.

studentName = ws.Cells(i, 2).Value

Here, 2 represents column B.

15. Reading the Course

Column C contains the course.

courseName = ws.Cells(i, 3).Value

The third argument represents column C.

16. Reading the Marks

Column D contains the student's marks.

marks = ws.Cells(i, 4).Value

Column D is represented by number 4.

17. Declaring Search Variables

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.

18. Displaying the Result

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.

19. Using Exit For

Once the student is found, there is no need to continue searching.

Exit For

This immediately stops the For loop.

20. Showing Record Not Found

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.

21. Complete Search Macro

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

22. Understanding the Search Flow

The macro follows these steps:

  1. Ask the user for Student ID.
  2. Open the Students worksheet.
  3. Find the last row.
  4. Start searching from row 2.
  5. Compare each Student ID.
  6. Read the matching record.
  7. Display the result.
  8. Show a not-found message if necessary.

23. Searching by Student Name

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.

24. Searching with Partial Text

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.

25. Highlighting the Found Record

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.

26. Using a Search Result Sheet

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.

27. Improving the Search System

A student search system can be improved by adding:

  • Search by Student ID
  • Search by Student Name
  • Search by Course
  • Search result worksheet
  • Clear search button
  • Record highlighting
  • Multiple matching records

28. Practical Example

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

29. Common Mistakes

While creating a search system, avoid these common mistakes:

  • Using the wrong worksheet name.
  • Searching the wrong column.
  • Starting the loop from row 1.
  • Forgetting to check empty input.
  • Forgetting the not-found condition.
  • Not using Exit For after finding a unique record.

30. Complete Student Search System

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.

📌 Key Points

  • InputBox can be used to accept a Student ID.
  • A For loop can search student records row by row.
  • Cells(row, column) can read values from a worksheet.
  • A Boolean variable can track whether a record was found.
  • Exit For can stop searching after finding a unique record.
  • MsgBox can display the search result.
  • VBA can also highlight the matching student row.
  • Student search is useful for building Excel-based management systems.

🧠 Quick Quiz

Question: Which VBA statement is useful for stopping a For loop after a student record has been found?