How to get a grade in C3?

Create an if-elseif chain that displays the correct letter grade for the student in C3.

Students final percent 99% E Grade Scale
A >= .90
B >= .80
C >= .70
D >= .60
E less than 60
the code i wrote:

Sub ifElseif()
' declaring variable
Dim sngPercentage As Double
Dim strGrade As String
'assign variable
sngPercentage = Range("B3")
strGrade = Range("C3")
sngPercentage = ActiveSheet.Range("B3").Value
If sngPercentage >= 90 And sngPercentage <= 100 Then
strGrade = "A"
ElseIf sngPercentage >= 80 Then
strGrade = "B"
ElseIf sngPercentage >= 70 Then
strGrade = "C"
ElseIf sngPercentage >= 60 Then
strGrade = "D"
Else
strGrade = "E"
Range("B3").Select
ActiveSheet.Range("c3") = strGrade
End If

End Sub

Almir's picture

CASE would execute faster than IF

Quick note: CASE would execute faster than IF. Instead of checking condition in each IF statement, CASE addresses directly to appropriate action.

Something like this:

Sub Testing2()

Dim sngPercentage As Double
Dim strGrade As Range '//// Instead of string change it to range because cell is also a range object
'assign variable

sngPercentage = Range("B3")
Set strGrade = Range("C3")

sngPercentage = ActiveSheet.Range("B3").Value

Select Case sngPercentage
Case Is >= 0.9
strGrade = "A"
Case Is >= 0.8
strGrade = "B"
Case Is >= 0.7
strGrade = "C"
Case Is >= 0.6
strGrade = "D"
Case Is < 0.6
strGrade = "E"
End Select

Range("B3").Activate
ActiveSheet.Range("C3") = strGrade

End Sub

Vikas Verma's picture

Try this

Hi Dear,

try this...Sub testing()
Dim sngPercentage As Double
Dim strGrade As Range '//// Instead of string change it to range because cell is also a range object
'assign variable
sngPercentage = Range("B3")
Set strGrade = Range("C3")
sngPercentage = ActiveSheet.Range("B3").Value
If sngPercentage >= 0.9 And sngPercentage <= 0.1 Then '//// compare with % value, 0.9 = 90%
strGrade = "A"
ElseIf sngPercentage >= 0.8 Then
strGrade = "B"
ElseIf sngPercentage >= 0.7 Then
strGrade = "C"
ElseIf sngPercentage >= 0.6 Then
strGrade = "D"
Else
strGrade = "E"
Range("B3").Select
ActiveSheet.Range("c3") = strGrade
End If

End Sub

Hope it will help you

Almir's picture

Change "sngPercentage <= 0.1 Then" to "sngPercentage <= 1 Then"

Vikas, I think that your:

"If sngPercentage >= 0.9 And sngPercentage <= 0.1 Then"
should be replaced by:

"If sngPercentage >= 0.9 And sngPercentage <= 1 Then"

It should be:
"1" instead of "0,1" (100%, not 10%)

Vikas Verma's picture

Thanks bro...

Thanks for correcting me...

Warm regards,
Vikas