简体   繁体   English

执行VBA功能时Excel错误#Value

[英]Excel Error #Value when executing VBA function

I've build a simple function to collect some values from a string. 我建立了一个简单的函数来从字符串中收集一些值。 Example string would be "abc(1.1);abc(1.2);bac(1.3)". 示例字符串为“ abc(1.1); abc(1.2); bac(1.3)”。 The goal is to get values and sum them up in the end and return them as the result. 目标是获取值并在最后将它们求和并返回它们作为结果。 Msgbox about "valor"(the total sum) works great and shows the correct value. 关于“勇气”(总和)的Msgbox效果很好,并显示正确的值。 But I get and #Value error on the cell when using this function. 但是使用此函数时,在单元格上出现#Value错误。 Can you help me understand why? 你能帮我理解为什么吗?

Function Organize(CellRef As String) As Double

Dim i As Integer
Dim lentotal As Long
Dim inicio As Long
Dim fim As Long
Dim a As Long
Dim valor As Double
Dim check As String
a = 1
x = CellRef
lentotal = Len(x)

i = Len(x) - Len(Replace(x, ";", ""))
Dim tech(1 To 100) As Double


Do While a <= i + 1
    inicio = InStr(x, "(")
    fim = InStr(x, ")")

    'MsgBox Replace((Mid(x, inicio + 1, fim - inicio - 1)), ".", ",")
    tech(a) = Replace((Mid(x, inicio + 1, fim - inicio - 1)), ".", ",")
    If Left(x, 4) = "ABC" Then
        valor = valor + tech(a)
    End If

    If a > i Then
        MsgBox valor
        Organize = valor
        End
    Else
        x = Right(x, Len(x) - fim - 1)
    End If

    a = a + 1
Loop


End Function

尝试使用Exit Function而不是End

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM