Excel VBA返回行为怪异,跳回来
Private Function GetLastSameRow(ByVal row, initialValue) As Integer . . . If (nextCell.Value = initialValue) Then GetLastSameRow = GetLastSameRow(row + 1, initialValue) End If MsgBox ("returning : " & row) GetLastSameRow = row End Function
在完成if语句后,行为真的很奇怪。
我运行它的debugging器,这是如何跳转:
1. End If ' 2. MsgBox ("returning : " & row) ' row value is 3 3. GetLastSameRow = row ' 4. MsgBox ("returning : " & row) ' row value is 2 ???????? 5. GetLastSameRow = row '
所以基本上它想要返回correct
值,但是,然后跳回到End if
从中得到correct-1
值。
你写了一个recursion函数! 如果您使用调用堆栈,您将看到debugging器将转到您的函数的另一个实例。 问题在于:
GetLastSameRow = GetLastSameRow(row + 1,initialValue)
这是调用该函数的另一个实例。