|
Macro Code for ready reference:
Sub calculating_grades()
'ask for the student's name
'initialize the starting row
x = 1
Cells(x, 1).Value = "Name"
Cells(x, 1).Font.Bold = True
Cells(x, 2).Value = "Marks1"
Cells(x, 2).Font.Bold = True
Cells(x, 3).Value = "Marks2"
Cells(x, 3).Font.Bold = True
Cells(x, 4).Value = "Marks3"
Cells(x, 4).Font.Bold = True
Cells(x, 5).Value = "Marks4"
Cells(x, 5).Font.Bold = True
Cells(x, 6).Value = "Marks5"
Cells(x, 6).Font.Bold = True
Cells(x, 7).Value = "Total"
Cells(x, 7).Font.Bold = True
Cells(x, 8).Value = "Percentage"
Cells(x, 8).Font.Bold = True
Cells(x, 9) = "Grade"
Cells(x, 9).Font.Bold = True
Line1:
x = x + 1
StudentName = InputBox("Enter the student's name")
Cells(x, 1).Value = StudentName
'assume 5 subjects
'initialize marks, total
marks = 0
total = 0
For counter = 1 To 5
marks = InputBox("Enter the student's marks in the 5 subjects")
Cells(x, (counter + 1)).Value = marks
total = total + marks
Cells(x, 7).Value = total
Dim percentage As Double
percentage = 0#
percentage = total / 5
Cells(x, 8).Value = percentage
If percentage >= 90 Then
Grade = "A"
ElseIf percentage >= 80 Then
Grade = "B"
ElseIf percentage >= 70 Then
Grade = "C"
ElseIf percentage >= 60 Then
Grade = "D"
Else
Grade = "Work Harder!"
End If
Cells(x, 9).Value = Grade
Next counter
If Grade = "A" Then
Cells(x, 9).Font.ColorIndex = 3
Cells(x, 9).Font.Bold = True
End If
question = InputBox("Do you want to continue? Enter 'y' or 'Y' for
Yes.")
If question = "y" Or question = "Y" Then
GoTo Line1
Else
End
End If
End Sub
|