使用Select Case语句的公共函数吐出#VALUE!错误

时间:2015-03-21 00:40:13

标签: excel vba debugging

根据我书中的陈述,它正在计算我选择作为选择案例字母的PV = NRT值。

Public Function ATHING(SolveFor As String, v1 As Single, v2 As Single, v3 As Single, v4 As Single) As Variant

Dim p As Single
Dim v As Single
Dim n As Single
Dim R As Single
Dim t As Single

Select Case SolveFor

Case "P"
    n = v1
    R = v2
    t = v3
    v = v4
ATHING = n * R * t / v

Case "V"
    n = v1
    R = v2
    t = v3
    p = v4
ATHING = n * R * t / p

Case "N"
    p = v1
    v = v2
    R = v3
    t = v4
ATHING= p * v / (R * t)

Case "R"
    p = v1
    v = v2
    n = v3
    t = v4
ATHING = p * v / (n * t)

Case "T"
    p = v1
    v = v2
    n = v3
    R = v4
ATHING = p * v / (n * R)

Case Else
ATHING = "CAN'T FIND VARIALBE TO SOLVE FOR, DO IT AGAIN"

End Select

End Function

如果我在Select Case的位置放了一个数字,它会返回Else Case。但如果我在其中放置一个Case的字母,它将返回#VALUE!错误,这意味着我某处输入的输入不正确。

该功能位于模块内部,因此不应该是问题。

有什么明显的吗?

1 个答案:

答案 0 :(得分:1)

解决方案,取代Case""有数字。 Excel不喜欢在其功能中包含字母和数字,不知道为什么。我使用0-4表示PV = NRT,因为它们出现在等式中。

Public Function ATHING(SolveFor As String, v1 As Single, v2 As Single, v3 As Single, v4 As Single) As Variant

Dim p As Single
Dim v As Single
Dim n As Single
Dim R As Single
Dim t As Single

Select Case SolveFor

Case "0"
    n = v1
    R = v2
    t = v3
    v = v4
ATHING = n * R * t / v

Case "1"
    n = v1
    R = v2
    t = v3
    p = v4
ATHING = n * R * t / p

Case "2"
    p = v1
    v = v2
    R = v3
    t = v4
ATHING= p * v / (R * t)

Case "3"
    p = v1
    v = v2
    n = v3
    t = v4
ATHING = p * v / (n * t)

Case "4"
    p = v1
    v = v2
    n = v3
    R = v4
ATHING = p * v / (n * R)

Case Else
ATHING = "CAN'T FIND VARIABLE TO SOLVE FOR, DO IT AGAIN"

End Select

End Function