Brandon Myers
Brandon Myers

Reputation: 1

VBA #Value Error in

I'm having a bit of a headache when it comes to VBA. I had tried to search online for an answer but to no luck. I have learned Python, but VBA is a different ballpark.

Dim X As String

Function GRADELETTER_PM(Num_Grade As Double)
X = "A"
If Num_Grade >= 0.93 Then 'finds corresponding letter to a grade'
    X = "A"
    MsgBox X

End If
If Num_Grade >= 0.9 Then
    X = "A-"

End If
If Num_Grade >= 0.88 Then
    X = "B+"

End If
If Num_Grade >= 0.83 Then
    X = "B"

End If
If Num_Grade >= 0.8 Then
    X = "B-"

End If
If Num_Grade >= 0.78 Then
   X = "C+"

End If
If Num_Grade >= 0.73 Then
    X = "C"

End If
If Num_Grade >= 0.7 Then
X = "C-"

End If
If Num_Grade >= 0.67 Then
    X = "D+"

End If
If Num_Grade >= 0.6 Then
    X = "D"

End If
If Num_Grade < 0.6 Then

    X = "F"

End If
 End Function

The program is supposed to calculate a grade to its letter. IE a 93% (input) is an "A" (Output) while a 64% is a "D". The only input is the grade. The sheet itself has multiple tables that do not aline themselves perfectly (ie, not the same Col x Row) and the formula will be used 40+ times on that one sheet when it works. Thanks in advance.

Upvotes: 0

Views: 49

Answers (1)

Lee Mac
Lee Mac

Reputation: 16015

I would suggest using a Select Case statement, for example:

Function GRADELETTER_PM(ByVal Num_Grade As Double) As String
    Select Case Num_Grade
        Case Is >= 0.93: GRADELETTER_PM = "A"
        Case Is >= 0.9:  GRADELETTER_PM = "A-"
        Case Is >= 0.88: GRADELETTER_PM = "B+"
        Case Is >= 0.83: GRADELETTER_PM = "B"
        Case Is >= 0.8:  GRADELETTER_PM = "B-"
        Case Is >= 0.78: GRADELETTER_PM = "C+"
        Case Is >= 0.73: GRADELETTER_PM = "C"
        Case Is >= 0.7:  GRADELETTER_PM = "C-"
        Case Is >= 0.67: GRADELETTER_PM = "D+"
        Case Is >= 0.6:  GRADELETTER_PM = "D"
        Case Else:       GRADELETTER_PM = "F"
    End Select
 End Function

Here is a reference.

Upvotes: 1

Related Questions