简体   繁体   中英

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)". 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. But I get and #Value error on the cell when using this function. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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