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.
A student result system is an Excel-based system that processes student marks and produces useful result information.
It can contain:
Manually calculating results for many students can be repetitive. VBA can automate the calculations and produce consistent results.
Automation can:
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 |
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.
Open the VBA Editor using:
Alt + F11
Then insert a standard module:
Insert → Module
Create a new procedure:
Sub GenerateResult()
End Sub
First, reference the Result worksheet.
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Result")
The variable ws now represents the Result worksheet.
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
English marks are stored in column C.
englishMarks = ws.Cells(i, 3).Value
The variable i represents the current student's row.
Computer marks are stored in column D.
computerMarks = ws.Cells(i, 4).Value
The current student's Computer marks are stored in the variable.
Math marks are stored in column E.
mathMarks = ws.Cells(i, 5).Value
The value is stored in the mathMarks variable.
The total marks are calculated by adding all three subject marks.
total = englishMarks + computerMarks + mathMarks
For example:
85 + 78 + 92 = 255
Column F is used for the total.
ws.Cells(i, 6).Value = total
The calculated total is written into the current student's row.
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%
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"
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.
Column I is used for the final result.
ws.Cells(i, 9).Value = result
The result will be either Pass or Fail.
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 |
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
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.
Column H is used for the grade.
ws.Cells(i, 8).Value = grade
The grade is stored in the current student's 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.
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.
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.
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
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
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.
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.
A complete student result system follows these steps:
Student Marks
↓
Validation
↓
Total
↓
Percentage
↓
Grade
↓
Pass / Fail
↓
Formatted Result
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.
Question: Which VBA statement is useful for assigning different grades based on percentage ranges?