簡體   English   中英

Excel中的VBScript遞歸導致堆棧不足錯誤

[英]OUT OF STACK Error with VBScript recursion in Excel

讓我詳細解釋一下。 所以這段代碼要做的是,基於參數值,如果'id'或'f'大於0並且'l'或's'為6,那么它將計算3%(分別在下一次迭代中為2%和1% )的值(例如= P),將“ id”和“ l”更新為其他值並將其存儲在其他位置。 直到“ id”或“ f”變為0或“ l”或“ s”大於8為止。

這就是它要做的全部。 但是它最多計算第一個值的2%並給出錯誤

 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

假設此代碼編寫正確(即所有變量的值均已正確設置),那么我看到的唯一問題是l的值和If l < 9 Then的條件。 可能的選項是:

  • 對於l<6您的代碼將不執行任何操作,並且將無限循環。 (幾乎)與

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

  • 對於6<=l<=8您的代碼將執行某些操作( If/ElseIf語句),但仍將無限循環,因為更改l值的唯一行是l = l + 1 ,它們僅在l=6運行並且l=7 因此,您可以獲得的最高l值為8。

  • 對於l>8您的代碼將什么都不做

我不知道此代碼的目的是什么,因此很難確定應如何解決。 也許在ElseIf l = 8 Then之后加上l = l + 1 ElseIf l = 8 Then就足夠了。 但是,這只能解決第二種情況。 如果您從l<6開始,您仍將永遠循環。

UPDATE

檢查下面的代碼。 我沒有檢查值是否寫在正確的單元格中,我猜您可以調試它,以防萬一某些東西以錯誤的方式顯示。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM