Lesson 54 of 60 – Automatic Total and Percentage in VBA
90%

Automatic Total and Percentage in VBA

In Excel, total marks and percentage are common calculations in student marksheets and result systems. VBA can automatically calculate these values for one student or for many students.

In this lesson, we will learn how to read subject marks, calculate the total, calculate the percentage, write the results into Excel cells, and process multiple student records using VBA.

Note: If there are three subjects and each subject has a maximum of 100 marks, the maximum total is 300. The percentage can be calculated using (Total / 300) * 100.

1. What is Total Marks?

Total marks are the sum of marks obtained in all subjects.

For example, if a student gets:

  • English = 80
  • Computer = 75
  • Math = 90

The total is:

80 + 75 + 90 = 245

2. What is Percentage?

Percentage represents the marks obtained compared with the maximum possible marks.

For three subjects with a maximum of 100 marks each:

Percentage = (Total / 300) * 100

If the total is 245:

(245 / 300) * 100 = 81.67%

3. Prepare the Worksheet

Create a worksheet named Marks with the following columns:

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

4. Enter Sample Marks

Enter sample data from row 2.

ID Name English Computer Math
ST101 Rahul 80 75 90
ST102 Amit 65 70 78

Columns F and G will contain the automatically calculated Total and Percentage.

5. Create a VBA Procedure

Open the VBA Editor using:

Alt + F11

Insert a standard module and create a procedure:

Sub CalculateTotalPercentage()

End Sub

6. Declare the Worksheet Variable

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Marks")

The ws variable now refers to the Marks worksheet.

7. Declare Calculation Variables

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

Dim total As Double
Dim percentage As Double

These variables store subject marks, total, and percentage.

8. Read English Marks

English marks are stored in column C.

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

This reads the value from C2.

9. Read Computer Marks

Computer marks are stored in column D.

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

This reads the value from D2.

10. Read Math Marks

Math marks are stored in column E.

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

This reads the value from E2.

11. Calculate the Total

Add the marks of all three subjects.

total = englishMarks + computerMarks + mathMarks

For example:

80 + 75 + 90 = 245

12. Write the Total into Excel

Column F is used for Total.

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

The calculated total is written into F2.

13. Calculate the Percentage

For three subjects with a maximum of 100 marks each:

percentage = (total / 300) * 100

If total is 245:

percentage = (245 / 300) * 100

percentage = 81.67

14. Write Percentage into Excel

Column G is used for Percentage.

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

This writes the calculated percentage into G2.

15. Format the Percentage

You can display the percentage with two decimal places.

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

For example, 81.666666 becomes 81.67.

16. Calculate Total and Percentage Together

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

total = englishMarks + computerMarks + mathMarks

percentage = (total / 300) * 100

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

This performs both calculations for the student in row 2.

17. Find the Last Student Row

To process multiple students, find the last used row.

Dim lastRow As Long

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

Column A is used to identify the last student record.

18. Use a For...Next Loop

A For...Next loop can process every student.

Dim i As Long

For i = 2 To lastRow

    'Calculation code

Next i

The variable i represents the current row.

19. Read Marks Inside the Loop

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

The current student's marks are read from columns C, D, and E.

20. Calculate Total Inside the Loop

total = englishMarks + computerMarks + mathMarks

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

The total is calculated and written into column F for every student.

21. Calculate Percentage Inside the Loop

percentage = (total / 300) * 100

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

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

Each student's percentage is automatically calculated and stored.

22. Complete Loop for Multiple Students

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

Next i

This processes all student rows automatically.

23. Handling Blank Rows

Before calculating, you can check whether the student ID exists.

If ws.Cells(i, 1).Value <> "" Then

    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

End If

This prevents calculations on completely blank student rows.

24. Validate Marks Before Calculation

Marks should normally be between 0 and 100.

If englishMarks < 0 Or englishMarks > 100 Then

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

End If

The same validation can be applied to the other subjects.

25. Practical Complete Total and Percentage System

The following program calculates Total and Percentage for all students.

Sub CalculateTotalPercentage()

    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("Marks")

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

    For i = 2 To lastRow

        If ws.Cells(i, 1).Value <> "" Then

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

            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

            total = englishMarks + computerMarks + mathMarks

            percentage = (total / 300) * 100

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

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

        End If

    Next i

    MsgBox "Total and Percentage Calculated Successfully"

End Sub

26. Using a Different Number of Subjects

The formula must be changed when the number of subjects changes.

For five subjects with a maximum of 100 marks each:

percentage = (total / 500) * 100

For six subjects:

percentage = (total / 600) * 100

Always use the correct maximum total for your marksheet.

27. Formatting the Total and Percentage

You can format the calculated columns to make the result easier to read.

With ws.Range("F1:G" & lastRow)

    .HorizontalAlignment = xlCenter
    .Borders.LineStyle = xlContinuous

End With

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

28. Best Practices for Total and Percentage

  • Keep original subject marks unchanged.
  • Store Total and Percentage in separate columns.
  • Use variables for calculations.
  • Use the correct maximum total.
  • Validate marks before calculating.
  • Use a loop for multiple students.
  • Find the last used row dynamically.
  • Format percentage values consistently.
  • Test the calculation with different marks.

29. Complete Total and Percentage Workflow

The complete automation process is:

  1. Identify the worksheet.
  2. Find the last student row.
  3. Read the subject marks.
  4. Validate the marks.
  5. Add all subject marks.
  6. Calculate the percentage.
  7. Write the total.
  8. Write the percentage.
  9. Format the calculated values.
  10. Repeat for every student.
Read Marks
    ↓
Validate Marks
    ↓
Calculate Total
    ↓
Calculate Percentage
    ↓
Write Results
    ↓
Format Results

30. Complete Understanding of Automatic Total and Percentage

Automatic calculation of Total and Percentage is one of the most useful applications of Excel VBA. Once the subject marks are available in the worksheet, VBA can calculate the results without requiring the user to manually enter formulas.

The basic total calculation is:

total = englishMarks + computerMarks + mathMarks

The percentage calculation for three subjects is:

percentage = (total / 300) * 100

The calculated values can then be written into Excel:

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

A For...Next loop allows the same calculations to be performed for many students. Validation can be added to prevent invalid marks, and formatting can be applied to make the final result easy to read.

This technique forms an important part of automated student marksheets, result systems, examination reports, and other Excel VBA projects.

📌 Key Points

  • Total marks are calculated by adding subject marks.
  • Percentage depends on the maximum possible marks.
  • For three subjects of 100 marks each, the maximum total is 300.
  • The percentage formula is (Total / Maximum Total) * 100.
  • VBA can read marks using Cells or Range.
  • Variables can store marks, total, and percentage.
  • For...Next can calculate results for multiple students.
  • The last used row can be found dynamically.
  • Marks should be validated before calculations.
  • Total and Percentage should be stored in separate columns.
  • NumberFormat can be used to display percentage values clearly.
  • Automatic calculations reduce repetitive manual work.

🧠 Quick Quiz

Question: If three subjects have a maximum of 100 marks each, which VBA formula correctly calculates percentage?