Lesson 53 of 60 – Creating a Student Result System with VBA
88%

Creating a Student Result System with VBA

A student result system is a practical Excel VBA project that can automatically process student marks and generate results. It can calculate total marks, percentage, grade, and pass or fail status.

In this lesson, we will build on the student marksheet project and create a more complete result system using Variables, Cells, Loops, If...Then...Else, Select Case, Reading Values, Writing Values, and Formatting.

Note: A result system can be customized according to the number of subjects, maximum marks, passing marks, grading rules, and report format used by your institution.

1. What is a Student Result System?

A student result system is an Excel-based system that processes student marks and produces useful result information.

It can contain:

  • Student ID
  • Student Name
  • Subject Marks
  • Total Marks
  • Percentage
  • Grade
  • Result

2. Why Create a Result System?

Manually calculating results for many students can be repetitive. VBA can automate the calculations and produce consistent results.

Automation can:

  • Save time
  • Reduce repetitive calculations
  • Calculate totals automatically
  • Calculate percentages automatically
  • Assign grades automatically
  • Determine Pass or Fail automatically

3. Prepare the Result Worksheet

Create a worksheet named Result and add the following headings.

Column Heading
A Student ID
B Student Name
C English
D Computer
E Math
F Total
G Percentage
H Grade
I Result

4. Enter Sample Student Data

Enter some sample marks from row 2.

ID Name English Computer Math
ST101 Rahul 85 78 92
ST102 Amit 65 72 68
ST103 Neha 35 62 70

Columns F through I will be generated automatically.

5. Open the VBA Editor

Open the VBA Editor using:

Alt + F11

Then insert a standard module:

Insert → Module

Create a new procedure:

Sub GenerateResult()

End Sub

6. Declare the Worksheet Variable

First, reference the Result worksheet.

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Result")

The variable ws now represents the Result worksheet.

7. Declare Result Variables

The program needs variables for marks and calculations.

Dim englishMarks As Double
Dim computerMarks As Double
Dim mathMarks As Double

Dim total As Double
Dim percentage As Double

Dim grade As String
Dim result As String

8. Read English Marks

English marks are stored in column C.

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

The variable i represents the current student's row.

9. Read Computer Marks

Computer marks are stored in column D.

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

The current student's Computer marks are stored in the variable.

10. Read Math Marks

Math marks are stored in column E.

mathMarks = ws.Cells(i, 5).Value

The value is stored in the mathMarks variable.

11. Calculate Total Marks

The total marks are calculated by adding all three subject marks.

total = englishMarks + computerMarks + mathMarks

For example:

85 + 78 + 92 = 255

12. Write the Total

Column F is used for the total.

ws.Cells(i, 6).Value = total

The calculated total is written into the current student's row.

13. Calculate Percentage

If three subjects have a maximum of 100 marks each, the maximum total is 300.

percentage = (total / 300) * 100

For a total of 255:

(255 / 300) * 100 = 85%

14. Write the Percentage

Column G contains the percentage.

ws.Cells(i, 7).Value = percentage

You can format it with two decimal places:

ws.Cells(i, 7).NumberFormat = "0.00"

15. Check Pass or Fail

Suppose the minimum passing mark is 40 in every subject.

If englishMarks >= 40 And _
   computerMarks >= 40 And _
   mathMarks >= 40 Then

    result = "Pass"

Else

    result = "Fail"

End If

This checks every subject before assigning the result.

16. Write the Result

Column I is used for the final result.

ws.Cells(i, 9).Value = result

The result will be either Pass or Fail.

17. Understanding Grades

A result system can also assign a grade based on percentage. For example, you can define your own grading rules.

Percentage Grade
90 or above A+
80 to 89.99 A
70 to 79.99 B
60 to 69.99 C
50 to 59.99 D
40 to 49.99 E
Below 40 F

18. Assign Grade Using If...ElseIf

If percentage >= 90 Then

    grade = "A+"

ElseIf percentage >= 80 Then

    grade = "A"

ElseIf percentage >= 70 Then

    grade = "B"

ElseIf percentage >= 60 Then

    grade = "C"

ElseIf percentage >= 50 Then

    grade = "D"

ElseIf percentage >= 40 Then

    grade = "E"

Else

    grade = "F"

End If

19. Assign Grade Using Select Case

Select Case can also be used to create a grading system.

Select Case percentage

    Case Is >= 90
        grade = "A+"

    Case Is >= 80
        grade = "A"

    Case Is >= 70
        grade = "B"

    Case Is >= 60
        grade = "C"

    Case Is >= 50
        grade = "D"

    Case Is >= 40
        grade = "E"

    Case Else
        grade = "F"

End Select

Select Case can make multiple percentage conditions easier to organize.

20. Write the Grade

Column H is used for the grade.

ws.Cells(i, 8).Value = grade

The grade is stored in the current student's row.

21. Find the Last Student Row

To process all students automatically, find the last used row.

Dim lastRow As Long

lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

This uses column A to identify the last student record.

22. Process Multiple Students

A For loop can process every student from row 2 to the last row.

Dim i As Long

For i = 2 To lastRow

    'Process current student

Next i

Each loop iteration processes one student's result.

23. Complete Result Calculation Loop

For i = 2 To lastRow

    englishMarks = ws.Cells(i, 3).Value
    computerMarks = ws.Cells(i, 4).Value
    mathMarks = ws.Cells(i, 5).Value

    total = englishMarks + computerMarks + mathMarks

    percentage = (total / 300) * 100

    ws.Cells(i, 6).Value = total
    ws.Cells(i, 7).Value = percentage

    If englishMarks >= 40 And _
       computerMarks >= 40 And _
       mathMarks >= 40 Then

        result = "Pass"

    Else

        result = "Fail"

    End If

    ws.Cells(i, 8).Value = grade
    ws.Cells(i, 9).Value = result

Next i

The grade calculation should be placed before writing the grade into column H.

24. Formatting the Result Header

The heading row can be formatted professionally.

With ws.Range("A1:I1")

    .Font.Bold = True
    .Font.Color = vbWhite
    .Interior.Color = RGB(0, 112, 192)
    .HorizontalAlignment = xlCenter
    .Borders.LineStyle = xlContinuous

End With

25. Practical Complete Student Result System

The following complete procedure calculates total, percentage, grade, and result for all students.

Sub GenerateResult()

    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long

    Dim englishMarks As Double
    Dim computerMarks As Double
    Dim mathMarks As Double

    Dim total As Double
    Dim percentage As Double

    Dim grade As String
    Dim result As String

    Set ws = ThisWorkbook.Worksheets("Result")

    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row

    For i = 2 To lastRow

        englishMarks = ws.Cells(i, 3).Value
        computerMarks = ws.Cells(i, 4).Value
        mathMarks = ws.Cells(i, 5).Value

        total = englishMarks + computerMarks + mathMarks

        percentage = (total / 300) * 100

        ws.Cells(i, 6).Value = total
        ws.Cells(i, 7).Value = percentage

        If percentage >= 90 Then

            grade = "A+"

        ElseIf percentage >= 80 Then

            grade = "A"

        ElseIf percentage >= 70 Then

            grade = "B"

        ElseIf percentage >= 60 Then

            grade = "C"

        ElseIf percentage >= 50 Then

            grade = "D"

        ElseIf percentage >= 40 Then

            grade = "E"

        Else

            grade = "F"

        End If

        ws.Cells(i, 8).Value = grade

        If englishMarks >= 40 And _
           computerMarks >= 40 And _
           mathMarks >= 40 Then

            result = "Pass"

        Else

            result = "Fail"

        End If

        ws.Cells(i, 9).Value = result

    Next i

    ws.Range("G2:G" & lastRow).NumberFormat = "0.00"

    With ws.Range("A1:I1")

        .Font.Bold = True
        .Font.Color = vbWhite
        .Interior.Color = RGB(0, 112, 192)
        .HorizontalAlignment = xlCenter
        .Borders.LineStyle = xlContinuous

    End With

    With ws.Range("A1:I" & lastRow)

        .Borders.LineStyle = xlContinuous

    End With

    ws.Columns("A:I").AutoFit

    MsgBox "Student Results Generated Successfully"

End Sub

26. Formatting Pass and Fail

You can visually highlight Pass and Fail results.

If ws.Cells(i, 9).Value = "Pass" Then

    ws.Cells(i, 9).Interior.Color = vbGreen
    ws.Cells(i, 9).Font.Color = vbWhite

Else

    ws.Cells(i, 9).Interior.Color = vbRed
    ws.Cells(i, 9).Font.Color = vbWhite

End If

This makes the result easier to identify.

27. Handling Invalid Marks

Before calculating a result, you can check whether marks are valid.

If englishMarks < 0 Or englishMarks > 100 Then

    MsgBox "Invalid English marks in row " & i
    Exit Sub

End If

If computerMarks < 0 Or computerMarks > 100 Then

    MsgBox "Invalid Computer marks in row " & i
    Exit Sub

End If

If mathMarks < 0 Or mathMarks > 100 Then

    MsgBox "Invalid Math marks in row " & i
    Exit Sub

End If

Validation helps prevent incorrect results.

28. Best Practices for a Result System

  • Keep original subject marks unchanged.
  • Use separate columns for calculated values.
  • Use variables for calculations.
  • Find the last student row dynamically.
  • Validate marks before calculating results.
  • Use clear grading rules.
  • Use consistent formatting.
  • Highlight Pass and Fail clearly.
  • Use worksheet-qualified references.
  • Test the program with different student marks.

29. Complete Result System Workflow

A complete student result system follows these steps:

  1. Prepare the student data worksheet.
  2. Read the subject marks.
  3. Validate the marks.
  4. Calculate total marks.
  5. Calculate percentage.
  6. Determine the grade.
  7. Determine Pass or Fail.
  8. Write the calculated values.
  9. Format the result.
  10. Repeat the process for every student.
Student Marks
      ↓
Validation
      ↓
Total
      ↓
Percentage
      ↓
Grade
      ↓
Pass / Fail
      ↓
Formatted Result

30. Complete Understanding of Student Result System

A student result system is a practical example of how different VBA concepts can work together in one project.

The program reads marks from the worksheet:

englishMarks = ws.Cells(i, 3).Value
computerMarks = ws.Cells(i, 4).Value
mathMarks = ws.Cells(i, 5).Value

It calculates the total:

total = englishMarks + computerMarks + mathMarks

It calculates the percentage:

percentage = (total / 300) * 100

It assigns a grade:

If percentage >= 90 Then
    grade = "A+"
ElseIf percentage >= 80 Then
    grade = "A"
ElseIf percentage >= 70 Then
    grade = "B"
ElseIf percentage >= 60 Then
    grade = "C"
ElseIf percentage >= 50 Then
    grade = "D"
ElseIf percentage >= 40 Then
    grade = "E"
Else
    grade = "F"
End If

Finally, it checks whether the student has passed all subjects and writes the result into the worksheet.

This project gives students practical experience with variables, worksheet objects, Cells, loops, conditions, calculations, and formatting. These concepts can later be used to build larger Excel VBA applications.

📌 Key Points

  • A student result system can be automated using VBA.
  • Subject marks can be read using Cells and Value.
  • Total marks can be calculated automatically.
  • Percentage can be calculated from total marks.
  • If...Then...Else or Select Case can be used for grading.
  • Each subject can be checked for the minimum passing marks.
  • For...Next can process multiple students.
  • The last used row can be detected dynamically.
  • Calculated results can be written into separate columns.
  • Pass and Fail results can be formatted differently.
  • Input validation helps prevent incorrect results.
  • A result system combines many important Excel VBA concepts.

🧠 Quick Quiz

Question: Which VBA statement is useful for assigning different grades based on percentage ranges?