VBA中的#Value错误

时间:2018-02-21 23:48:35

标签: excel vba excel-vba

对于VBA,我有点头疼。我曾尝试在线搜索答案,但没有运气。我学过Python,但VBA是一个不同的球场。

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

该程序应该计算其字母的等级。 IE 93%(输入)是"A"(输出),而64%是"D"。唯一的输入是等级。工作表本身有多个表格,这些表格不能完美地对齐(即,不是相同的Col x Row),并且当工作时,该公式将在该工作表上使用40次以上。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我建议使用Select Case语句,例如:

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是一个参考。