Lesson 52 of 60 – Creating a Student Marksheet with VBA
87%

Creating a Student Marksheet with VBA

A student marksheet is a practical Excel project that can be automated using VBA. Instead of manually entering totals and calculating results, VBA can automatically read student marks, calculate the total, percentage, and result, and place the information into the worksheet.

In this lesson, we will combine the VBA concepts learned so far, including Variables, Cells, Range, If...Then...Else, For...Next, Reading Values, Writing Values, and Formatting Cells.

Note: This lesson is a practical project. We will create a simple marksheet with Student ID, Student Name, subject marks, Total, Percentage, and Result.

1. What is a Student Marksheet?

A student marksheet is a worksheet that stores the marks and result information of students.

A simple marksheet may contain:

  • Student ID
  • Student Name
  • English Marks
  • Computer Marks
  • Math Marks
  • Total
  • Percentage
  • Result

2. Why Automate a Marksheet?

Calculating results manually for many students can be repetitive. VBA can automate these calculations.

VBA can automatically:

  • Read marks from cells
  • Calculate total marks
  • Calculate percentage
  • Determine Pass or Fail
  • Write the result into cells
  • Format the marksheet

3. Prepare the Marksheet Columns

First, create the following headings in a worksheet named Marksheet.

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

4. Enter Sample Student Data

Enter some sample student data from row 2.

Student ID Student Name English Computer Math
ST101 Rahul 75 82 68
ST102 Amit 65 70 72

Columns F, G, and H will be calculated automatically.

5. Create the VBA Procedure

Open the VBA Editor using:

Alt + F11

Insert a standard module and create a procedure:

Sub GenerateMarksheet()

End Sub

The marksheet automation code will be written inside this procedure.

6. Declare the Worksheet Variable

First, create a worksheet variable.

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Marksheet")

The variable ws now refers to the Marksheet worksheet.

7. Declare the Required Variables

We need variables for the marks and calculations.

Dim englishMarks As Double
Dim computerMarks As Double
Dim mathMarks As Double
Dim total As Double
Dim percentage As Double

These variables will temporarily store values during the calculation.

8. Read English Marks

English marks are stored in column C. For the first student, read the value from C2.

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

Cells(2, 3) represents C2.

9. Read Computer Marks

Computer marks are stored in column D.

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

This reads the Computer marks of the student in row 2.

10. Read Math Marks

Math marks are stored in column E.

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

This reads the Math marks from E2.

11. Calculate Total Marks

The total is calculated by adding the marks of all three subjects.

total = englishMarks + computerMarks + mathMarks

If the marks are 75, 82, and 68, the total is:

75 + 82 + 68 = 225

12. Write the Total into the Worksheet

The Total column is column F.

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

The calculated total is written into F2.

13. Calculate Percentage

If there are three subjects and each subject has a maximum of 100 marks, the maximum total is 300.

percentage = (total / 300) * 100

For a total of 225:

(225 / 300) * 100 = 75%

14. Write Percentage into the Worksheet

The Percentage column is column G.

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

The calculated percentage is written into G2.

15. Determine Pass or Fail

Suppose a student must score at least 40 marks in every subject to pass.

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

    ws.Cells(2, 8).Value = "Pass"

Else

    ws.Cells(2, 8).Value = "Fail"

End If

The result is written into column H.

16. Format the Percentage

The percentage can be displayed with two decimal places.

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

For example, 75 becomes 75.00.

17. Use a For Loop for Multiple Students

Instead of processing only row 2, a For loop can process many students.

Dim i As Long

For i = 2 To 10

    'Process student

Next i

The variable i represents the current student row.

18. Read Marks Inside the Loop

The current row can be used to read the marks.

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

The program reads marks from columns C, D, and E for every student.

19. Calculate Total Inside the Loop

The total can be calculated for every student.

total = englishMarks + computerMarks + mathMarks

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

The result is written into column F of the current student's row.

20. Calculate Percentage Inside the Loop

percentage = (total / 300) * 100

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

Each student's percentage is calculated and written into column G.

21. Calculate Result Inside the Loop

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

    ws.Cells(i, 8).Value = "Pass"

Else

    ws.Cells(i, 8).Value = "Fail"

End If

This checks every subject before assigning the final result.

22. Find the Last Student Row

Instead of always processing rows 2 to 10, we can automatically find the last student row.

Dim lastRow As Long

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

This finds the last used row based on column A.

23. Complete 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

        ws.Cells(i, 8).Value = "Pass"

    Else

        ws.Cells(i, 8).Value = "Fail"

    End If

Next i

24. Format the Marksheet Header

The heading row can be formatted using a With block.

With ws.Range("A1:H1")

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

End With

This creates a professional-looking header.

25. Practical Complete Student Marksheet

The following program combines the main concepts into a complete student marksheet generator.

Sub GenerateMarksheet()

    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

    Set ws = ThisWorkbook.Worksheets("Marksheet")

    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 englishMarks >= 40 And _
           computerMarks >= 40 And _
           mathMarks >= 40 Then

            ws.Cells(i, 8).Value = "Pass"

        Else

            ws.Cells(i, 8).Value = "Fail"

        End If

    Next i

    With ws.Range("A1:H1")

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

    End With

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

        .Borders.LineStyle = xlContinuous

    End With

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

    MsgBox "Marksheet Generated Successfully"

End Sub

26. Common Marksheet Mistakes

  • Using the wrong subject column.
  • Using the wrong maximum marks.
  • Calculating the percentage incorrectly.
  • Checking only total marks instead of individual subjects.
  • Processing the wrong rows.
  • Overwriting the original marks.
  • Not checking for blank student records.
  • Forgetting to format the result.

27. Formatting Pass and Fail Results

You can use different formatting for Pass and Fail results.

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

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

Else

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

End If

This makes the result easier to identify visually.

28. Best Practices for a Student Marksheet

  • Keep the column headings clear.
  • Use a separate column for each subject.
  • Do not overwrite original marks when calculating results.
  • Use variables for calculations.
  • Use a loop for multiple students.
  • Find the last used row dynamically.
  • Validate marks before calculating results.
  • Use consistent formatting.
  • Use meaningful worksheet and variable names.

29. Complete Marksheet Workflow

A complete automated marksheet follows these steps:

  1. Prepare the worksheet headings.
  2. Enter or import student marks.
  3. Identify the last student row.
  4. Read the subject marks.
  5. Calculate the total.
  6. Calculate the percentage.
  7. Determine Pass or Fail.
  8. Write the calculated values.
  9. Format the marksheet.
  10. Display a completion message.
Read Marks
    ↓
Calculate Total
    ↓
Calculate Percentage
    ↓
Check Result
    ↓
Write Result
    ↓
Format Marksheet

30. Complete Understanding of Student Marksheet

Creating a student marksheet is an excellent practical exercise for learning Excel VBA because it combines many programming concepts in one project.

The program reads marks:

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

Then it calculates the total and percentage:

total = englishMarks + computerMarks + mathMarks

percentage = (total / 300) * 100

Then it determines the result:

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

    ws.Cells(i, 8).Value = "Pass"

Else

    ws.Cells(i, 8).Value = "Fail"

End If

Finally, VBA can format the complete marksheet automatically.

This project prepares you for more advanced Excel VBA applications such as student result systems, automatic reports, data-entry forms, and complete management projects.

📌 Key Points

  • A student marksheet can be automated using VBA.
  • Subject marks can be read using Cells or Range.
  • Variables can store marks and calculation results.
  • Total marks can be calculated automatically.
  • Percentage can be calculated using the total and maximum marks.
  • If...Then...Else can determine Pass or Fail.
  • For...Next can process multiple students.
  • The last student row can be detected dynamically.
  • VBA can write calculated values into the worksheet.
  • VBA can format the marksheet automatically.
  • Pass and Fail results can have different formatting.
  • A marksheet project combines many important VBA concepts.

🧠 Quick Quiz

Question: If three subjects each have a maximum of 100 marks, what formula can be used to calculate percentage?