VBA中的自定义函数出现#VALUE错误

时间:2018-08-23 06:30:30

标签: excel vba

下面是我的代码,我试图返回“ std”的值,但它给出了#VALUE错误。 我给“ strng”输入的字符串是3,3,3,-,3 我期望的结果是7。

Dim strng As Double
Dim std, i As Long
---------------------------------------------------
Function defaulters(strng) As Double
std = 4
a = Split(strng, ",")
    For i = 0 To Len(strng)
      If a(i) = "-" Then
        std = std - 1
      Else
        std = std + 1
      End If
    Next i
defaulters = std
End Function

2 个答案:

答案 0 :(得分:0)

您的For应该是这样

For i = LBound(a) To UBound(a)

整个事情是:

Function defaulters(strng) As Double
std = 4
a = Split(strng, ",")
    For i = LBound(a) To UBound(a)
      If a(i) = "-" Then
        std = std - 1
      Else
        std = std + 1
      End If
    Next i
defaulters = std
End Function

根据您在评论部分中发现的问题,您的代码逻辑应按以下方式更新

Function defaulters(strng) As Double
std = 4
a = Split(strng, ",")
    For i = LBound(a) To UBound(a)
        If a(i) = "-" Then
            std = std - 1
        Else
            std = std + Len(a(i))
        End If
    Next i
defaulters = std
End Function

答案 1 :(得分:0)

对此进行测试:

wmi