This version is cleaner and easier to read. The conditions are evaluated top-down, so once a condition is met, the subsequent conditions won't be checked.
Function GRADE(ByVal cellref As Range) As String
Dim cr As Integer
cr = CInt(cellref.Value)
If cr >= 95 Then
GRADE = "A++"
ElseIf cr >= 90 Then
GRADE = "A+"
ElseIf cr >= 85 Then
GRADE = "A"
ElseIf cr >= 80 Then
GRADE = "B++"
ElseIf cr >= 75 Then
GRADE = "B+"
ElseIf cr >= 70 Then
GRADE = "B"
ElseIf cr >= 60 Then
GRADE = "C"
ElseIf cr >= 50 Then
GRADE = "D"
ElseIf cr >= 40 Then
GRADE = "E"
Else
Excel formulas provide an option without needing macros enabled. Nested IF
statements are understandable but require multiple levels. LOOKUP
is a more concise approach.
For example, the formula (in cell B2) converts a score to a grade:
=LOOKUP(A2,{0,40,50,60,70,75,80,85,90,95},{"U","E","D","C","B","B+","B++","A","A+","A++"})
An alternative is placing the score and grade ranges on the sheet, then naming them _Score
and _Grade
. The formula simplifies to:
=LOOKUP(A2,_Score,_Grade)
Leveraging LOOKUP and defined names keeps the formula tidy.