简体   繁体   中英

OUT OF STACK Error with VBScript recursion in Excel

Let me explain it in detail. so what this code does is, based on the parameter values if 'id'or 'f' is greater than 0 and 'l' or 's' is 6 then it calculates the 3% (2% and 1% in next iterations respectively) of a value (say = P), updates the 'id' and 'l' to something else and and stores it somewhere else. This is to be done until 'id' or 'f' becomes 0 or 'l' or 's' becomes greater than 8.

That is all that it needs to be done. But it calculates up to 2% of the first value and gives the error

 Function percentage(f, s, t)
 Dim id, l, k, y
 k = 2
 id = f
 l = s
 y = t
        If id <> 0 Then

           While IsEmpty(Sheet7.Cells(k, 1).Value) = False

               If Sheet7.Cells(k, 1).Value = id Then
                   id = Sheet7.Cells(k, 11).Value
                      If l = 6 Then
                        Sheet7.Cells(k, l).Value = 0.03 * Sheet7.Cells(y, 3).Value
                         l = l + 1
                      ElseIf l = 7 Then
                        Sheet7.Cells(k, l).Value = 0.02 * Sheet7.Cells(y, 3).Value
                         l = l + 1
                       ElseIf l = 8 Then
                        Sheet7.Cells(k, l).Value = 0.01 * Sheet7.Cells(y, 3).Value
                       End If
                End If
                k = k + 1
                Wend
              percentage = percentage(id, l, y)
        End If

  End Function

Assuming this code is well written (ie all variables have their values set properly), the only problem I can see is the value of l and the condition of If l < 9 Then . Possible options are:

  • for l<6 - your code will do nothing and will just loop infinitely. It would be (almost) the same as

    Function percentage(f, s, t) percentage(f, s, t)

  • for 6<=l<=8 - your code will do something ( If/ElseIf statements) but will still loop infinitely, as the only lines that change the value of l are l = l + 1 which are run only for l=6 and l=7 . Hence, the highest l value you can achieve is 8.

  • for l>8 - your code will do nothing at all

I don't know what is the purpose of this code, so it's hard to tell how it should be fixed. Maybe adding l = l + 1 after ElseIf l = 8 Then is enough. However, this will fix only the second case. If you start with l<6 you will still loop forever.

UPDATE

Check the code below. I didn't check if values are written in right cells, I guess you can debug it in case something is displayed in wrong way.

Function percentage(f, s, t)
Dim id, l, k, y
k = 2
id = f
l = s
y = t

If id <> 0 Then
    If l > 5 And l < 9 Then
        While IsEmpty(Sheet7.Cells(k, 1).Value) = False

            If Sheet7.Cells(k, 1).Value = id Then
                id = Sheet7.Cells(k, 11).Value
                If l = 6 Then
                    Sheet7.Cells(k, l).Value = 0.03 * Sheet7.Cells(y, 3).Value
                    l = l + 1
                ElseIf l = 7 Then
                    Sheet7.Cells(k, l).Value = 0.02 * Sheet7.Cells(y, 3).Value
                    l = l + 1
                ElseIf l = 8 Then
                    Sheet7.Cells(k, l).Value = 0.01 * Sheet7.Cells(y, 3).Value
                    l = l + 1
                End If
            End If
            k = k + 1
        Wend
        percentage = percentage(id, l, y)
    End If
End If
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