简体   繁体   中英

Custom Function in EXCEL not working if return or output is out of FOR loop

Function DISCOUNT(quantity) ' "1-2,3,4,5-10,23" is the data in <quantity>
    Dim LString As String
    Dim LArray() As String
    Dim Daysfromto() As String
    Dim Size As Double
    Dim Days As Double
    Dim Totaldays As Double


        Totaldays = 0
        LString = quantity
        LArray = Split(LString, ",")
        Size = UBound(LArray) - LBound(LArray) + 1
                For i = 0 To Size
                        Contains = InStr(LArray(i), "-")
                        If Contains = 2 Then
                            Daysfromto = Split(LArray(i), "-")
                            Totaldays = Totaldays + Daysfromto(1) - Daysfromto(0) + 1
                        ElseIf Contains = 0 Then
                            Totaldays = Totaldays + 1
                        End If
                        MSGBOX Totaldays ' this works here
                Next i
    MSGBOX Totaldays ' this does not work here
    DISCOUNT = Totaldays ' this does not work here
End Function
LArray = Split(LString, ",")

By default, this creates a zero-based 1-D array. For your example this would be LArray(0 to 4) which is a total of 5 array elements. Zero, one, two, three, four is 5 array elements.

When you use,

Size = UBound(LArray) - LBound(LArray) + 1

... this is the same as Size = 4 - 0 + 1 which correctly shows 5 array elements. However, when you use,

For i = 0 To Size

... you trying to access a total of 6 array elements with LArray(i) . Zero, one, two, three, four, five is six array elements, not 5.

Solution:

Always use,

for i = lbound(LArray) to ubound(LArray)
    ...
next i

You will never go out of bounds with that method.

  1. Use Option Explicit .
  2. Don't use On Error Resume Next .
  3. 'not working' is neither a valid error code nor error description.

Code rewrite

Option Explicit

Sub main()
    Debug.Print DISCOUNT("1-2,3,4,5-10,23")
End Sub

Function DISCOUNT(quantity) ' "1-2,3,4,5-10,23" is the data in <quantity>
    Dim LString As String
    Dim LArray() As String
    Dim Daysfromto As Variant
    Dim Days As Double
    Dim Totaldays As Double
    Dim i As Long, Contains As Long


    Totaldays = 0
    LString = quantity
    LArray = Split(LString, ",")

    For i = LBound(LArray) To UBound(LArray)
            Contains = InStr(LArray(i), "-")
            If Contains > 0 Then
                Daysfromto = Split(LArray(i), "-")
                Totaldays = Totaldays + CLng(Daysfromto(1)) - CLng(Daysfromto(0)) + 1
            ElseIf Contains = 0 Then
                Totaldays = Totaldays + 1
            End If
            'Debug.Print Totaldays  ' this works here
    Next i

    'Debug.Print Totaldays
    DISCOUNT = Totaldays

End Function

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